Casting a ray out of the front of a part

I recently started to tinker around with rays and tried to create a ray that starts from a part and continues in the direction the front is facing. And it worked until I moved the part further away from 0. The hit point of the ray started to shift for some weird reason, I don’t know why it happens or what is causing it. I’m assuming this has something to do with my calculations.

In the image below it works fine (stud part is the ray and neon part is where the ray hits a part)

And here the hit point randomly shifts to the right

image

To calculate the ray I use this: Ray.new(Part.Position, Part.Position + (Part.CFrame.LookVector * 5000))

If someone could explain why this is happening or what I’m doing wrong it would help me a lot.

2 Likes

Did you ever figure this out? If not I could possibly be of some help.

I never did, and if you could help me that would be great!

Well, for starters I’d check this wiki out if you haven’t yet. Someone is typing right now so they may provide a better answer!

1 Like

I have read that already and I understand how ray works but I just dont get why the hit shifts.

1 Like

Use this:

Ray.new(Part.Position, Part.CFrame.LookVector * 5000)

Reasoning:

Ray.new() takes two arguments:

The direction is, normally, a unit vector (vector with magnitude of 1) based around the origin that dictates where you want the ray to point.

The Ray.new constructor’s second argument is technically a line segment (in your case, it has a length of 5000 - though I’m pretty sure raycasting in Roblox limits you to 1000 studs). With this in mind, you have a direction (unit) vector defined by Part.CFrame.LookVector, and multiplying this direction by a scalar (e.g, 5000) will extend the ray outwards in that direction from the origin (the first argument).

16 Likes

Perfect! Thanks for helping.

Thanks for the help, I just understood it wrong then.

2 Likes