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

Help with raycasting from a specific point?

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 answer

✓ Accepted answer

You did everything right. It's just that when you shoot the ray, it hits the pointFront part and stops.

To make it ignore the part, you just have to use the second argument of FindPartOnRay. The ray will ignore all of the descendants of this argument. Therefore, if we make the argument equal to the character, any part inside of the character (like pointFront) won't stop the ray.

local part, position = workspace:FindPartOnRay(ray, script.Parent)

On a side note, use variables! It will save you so much typing.

1 comment

0

Thank you!

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