Hi . I’m trying to create a plane crash minigame, and the plane needs to face to the part, because that’s the point where the plane is going to crash, what i mean is a part aiming another part, but my problem is that i don’t know how i could do that. Correct me if i’m wrong, but i think it uses LookVector, i don’t know how to use it, i already searched stuff about this, however, i didn’t find something useful or “understandable”. Thanks for reading.
I think this is what you are looking for.
Since CFrame.new()
is deprecated, it is replaced by CFrame.fromMatrix()
. Of course you can still used the deprecated version, but hey, is that a good practice?
Here’s a sample of that latter constructor:
--target is where to face, and eye is position, sort of like CFrame.new()
function lookAt(target, eye)
local forwardVector = (eye - target).Unit --lookVector
local upVector = Vector3.new(0, 1, 0) --model a vector going straight up
-- you need to know the right-hand rule, which has something to do with counter-clockwise rotation (search it up for more info)
local rightVector = forwardVector:Cross(upVector) --cross two vectors in that order to get the right vector
local upVector2 = rightVector:Cross(forwardVector) --get the REAL up vector perpendicular to the other vectors
return CFrame.fromMatrix(eye, rightVector, upVector2) --return that CFrame
end
Eye and Target need to be Vector3
s!
Have you searched the DevForum if there are already existing articles? I’m pretty sure there are a few articles regarding similar problems.
Just for clarity: CFrame.new
itself isn’t deprecated. The overload CFrame.new(Vector3 pos, Vector3 lookAt)
is deprecated, and the lookAt()
function’s arguments are in reverse: the look goes first, the position goes second.
When i get the CFrame returned:
How do i apply it to the part’s orientation?
@OptimisticSide - Yeah, i just said it in the post , sorry if i misunderstood you:
It’s ok, I also misread your post. For that, I apologize.
I got an issue:
local OrientationLook = lookAt(e.Position, Char:FindFirstChild("HumanoidRootPart").Position)
e.Orientation = CFrame:ToOrientation(OrientationLook)
ToOrientation
is a nil value, should i replace that with FromOrientation?