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?
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.
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.