Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can I Attach a Ray to a Part?

Asked by 5 years ago

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
short answer: no. long answer: not literally Fifkee 2017 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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)
0
you should credit the wiki Fad99 286 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

Answer this question