How do I rotate a model so that it faces a lookvector?

I am trying to rotate a model so that it faces a lookvector

I’ve done something like this:

item.Parent:SetPrimaryPartCFrame(CFrame.new(item.Parent:GetPrimaryPartCFrame().p, lookvector))

however this doesn’t seem to work either and i’m not too sure what else to do.

Hello again!

Now the CFrame.new(Position : Vector3, LookAt : Vector3) overload is actually deprecated and shouldn’t be really be used. Instead I recommend the use of CFrame.fromMatrix, this takes a rightVector and a upVector which then it’ll create a LookVector via these two provided Vectors.

You can learn more about it here, it provides a function that you can use.

1 Like

Hello again indeed :smiley:

I’ve tried to use some source code from that and ended up with this:

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)

    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

item.Parent:SetPrimaryPartCFrame(lookAt(lookvector, item.Position))

However, still does not rotate in the direction.

If this helps:

  • I apply a Velocity (Directly to the primarypart without use of body movers)
  • I also use a BodyForce (which i use to add an anti-gravity to the part)

Thank you for supplying me with help!

1 Like

Ah I noticed it produces the CFrame to be inverted, make sure to make the rightVector negative:

return CFrame.fromMatrix(eye, -rightVector, upVector2)
1 Like

I’ve done that, however still. The Rotation is not applied to the model

1 Like

Did you ensure that you set a PrimaryPart? Also, what happens if you don’t apply Velocity and the BodyForce?

1 Like

This is a freeze frame (Sorry for the wait) :

https://gyazo.com/2b89aa04923e59eab67be87f260f9b9f

PrimaryPart is set and me anchoring the part should cancel the velocity.

conclusion is, the orientation is changed just not the right way.
(I thought rotation wasn’t being applied but it was my bad.)

This is a technical mistake. It was deprecated because we intended to add a CFrame.new(Position: Vector3, LookAt: Vector3, [UpDirection: Vector3]) overload to CFrame.new, which lets you explicitly specify the up direction, but that got put on the back burner and forgotten about.

Re-implementing it yourself when you need a use case of exactly that is error-prone and a waste of time, please continue using CFrame.new(at, lookta), as the new variant is on it’s way down the pipe now.

6 Likes