0
\$\begingroup\$

I'm creating a third-person game. I want the player-model to follow the rotation of the camera. I do that with this :

var camRotation = new Quaternion(cam.rotation.x, cam.rotation.y, cam.rotation.z, cam.rotation.w);
transform.rotation = camRotation;

The horizontal axis works fine, and the model turns with my camera, but the mode vertical axis also tilts up and down, which i do not want.

I've tried zeroing out the rotation.y and z, but this has some strange effects when I do.

\$\endgroup\$
  • \$\begingroup\$ Remember that rotation quaternions are points on a 4-dimensional unit sphere. It doesn't make sense to zero one component of the quaternion, because that rips it off that sphere. In general, you do not want to change individual components of a quaternion by hand — there are really only two operations it makes sense to do that way: inverting a quaternion or rotating to the same orientation in the opposite direction, and the former is clearer to do with Quaternion.Inverse() anyway. \$\endgroup\$ – DMGregory Jan 8 at 11:28
0
\$\begingroup\$

Take the camera's forward direction, and flatten it into the plane of rotation you want.

Then form a quaternion looking in that direction.

var flatForward = camera transform.forward;
flatForward.y = 0;

transform.rotation = Quaternion.LookRotation{flatForward);

Here I assume you want the world horizontal plane, but we can also make this work with the plane of the ground under the player.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.