Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can I Attach a Ray to a Part?

So I have been using raycasting to detect obstacles in front of moving parts when they come in contact with them; however, this has required me to constantly create new rays for every stud the part moves (which I feel takes up a lot of processing power). Is there a way to simply create one ray that stays attached to the position of a part? So far this is what I've been using:

local ray = Ray.new(Push.CFrame.p, Push.CFrame.lookVector * 2)

1 comment

1

short answer: no. long answer: not literally

2 answers

You can create a part on a ray. Here's the code to do that

local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
		
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		
game:GetService("Debris"):AddItem(beam, 0.1)

2 comments

0

you should credit the wiki

0

i think what you try to do is to create a "eye" for an npc or a "sensor" for a moving part.

if that is the purpose, the answer has nothing to do with ray, ray is just closest thing people can associate to a beam.

I do it this way for my landmine, and most of other stuff, create a invisible part in front of the npc, and weld it to npc, so the invisible part is attached to the moving part(say the head), when it touch something, a touch event been triggered, and the part name is passed into the function, so you "see" it

This discussion is preserved as it was. New answers, comments, and votes are closed.