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

How do I apply lookVector for rays?

Hello I am using raycasting and I want the ray to always go to the front. I really tried to understand lookVector and googled about it, but I just don't understand it. Can someone help me?

3 comments

1

Yes, the *500 changes the magnitude of the vector so that the ray travels 500 studs in the direction

1

The max distance for a ray in roblox is 1000

0

Thank you a lot

2 answers

✓ Accepted answer

lookVector is a property of CFrame that describes the forward-direction of its orientation as a unit vector (a vector whose magnitude is one). It does not describe a point that the CFrame is looking at.

For example, if a part's front-facing side was facing directly upwards, its lookVector would be:

Vector3.new(0, 1, 0)

If its front-facing side was facing directly towards the x-axis, its lookVector would be:

Vector3.new(1, 0, 0)

Again, this only describes the direction that it is facing. It doesn't matter where in the world your CFrame's position is located. If its front-facing side is pointing upwards, its lookVector will always be Vector3.new(0, 1, 0).

Knowing this, we can construct a Ray that shoots in the direction that a CFrame is pointing towards.

For this example, assume part is defined as a BasePart in workspace.

local cf = part.CFrame

local ray = Ray.new(cf.p, cf.lookVector)

According to the Ray constructor definition, we created a Ray whose origin is at the position of the part and is shooting one stud (since the magnitude of a lookVector is always 1) in the direction that it is pointing.

Similarly, rightVector is a property that describes the rightward-direction of a CFrame's orientation, and I, upVector, am a property that describes the upward-direction of a CFrame's orientation.

1 comment

1

that wasnt funny

Let's say you have a part and you want a ray to go where that part is looking.

This task is really simple because Ray.new takes two arguments, an origin and a direction, and we already have both.

local ray = Ray.new(part.Position, part.CFrame.lookVector* 500)

Because the ray starts at the center of the part, if you wanted to use FindPartOnRay you should use FindPartOnRayWithIgnoreList and add the part to the list.

2 comments

0

So the *500 is the distance?

0

congrats at 1000 points btw.

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