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:
1 | local ray = Ray.new(Push.CFrame.p, Push.CFrame.lookVector * 2 ) |
You can create a part on a ray. Here's the code to do that
01 | local part, position = workspace:FindPartOnRay(ray, player.Character, false , true ) |
02 |
03 | local beam = Instance.new( "Part" , workspace) |
04 | beam.BrickColor = BrickColor.new( "Bright red" ) |
05 | beam.FormFactor = "Custom" |
06 | beam.Material = "Neon" |
07 | beam.Transparency = 0.25 |
08 | beam.Anchored = true |
09 | beam.Locked = true |
10 | beam.CanCollide = false |
11 |
12 | local distance = (tool.Handle.CFrame.p - position).magnitude |
13 | beam.Size = Vector 3. new( 0.3 , 0.3 , distance) |
14 | beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new( 0 , 0 , -distance / 2 ) |
15 |
16 | game:GetService( "Debris" ):AddItem(beam, 0.1 ) |
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