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

How do I apply lookVector for rays?

Asked by
Bertox 159
5 years ago

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?

1
Yes, the *500 changes the magnitude of the vector so that the ray travels 500 studs in the direction theCJarmy7 1293 — 5y
1
The max distance for a ray in roblox is 1000 theCJarmy7 1293 — 5y
0
Thank you a lot Bertox 159 — 5y

2 answers

Log in to vote
3
Answered by
nilVector 812 Moderation Voter
5 years ago
Edited 5 years ago

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
that wasnt funny unmiss 337 — 5y
Ad
Log in to vote
2
Answered by
theCJarmy7 1293 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
So the *500 is the distance? Bertox 159 — 5y
0
congrats at 1000 points btw. Zafirua 1348 — 5y

Answer this question