My next part of my ROBLOX LUA journey has brought me to raycasts. I've been messing around with them, however I'm stuck with what I'm doing right now. I'm trying to make a raycast that gets projected out from an NPC's head outwards. Here's the code:
local ray = Ray.new(script.Parent.Head.CFrame.p, (script.Parent.pointFront.CFrame.p - script.Parent.Head.CFrame.p).unit * 999)
local part, position = workspace:FindPartOnRay(ray)
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 = (script.Parent.Head.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(script.Parent.Head.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
The code is a little sloppy, I understand. Most of it I copied from the Wiki on Raycasting and tried to apply it to what I'm doing, but it's not working. The specific problem I'm having is that they ray is being created, but it's only traveling from the NPC's head to the "pointFront" (which is located directly infront of the NPC's face). It's not extending outwards 999 studs like I want it to.
I understand that it's probably an easy solution that I'm missing, and that I probably can do this in a way simpler way, but hey, I'm just learning about raycasting. So all help (and advice) is greatly appreciated!

1 comment
Thank you!