Rotation of Camera = Transform on the y-axis?

I am trying to make a script for a player controller. I have a small problem with the rotation though. I have a separate code to make the camera look around (sample program in Unity) and the camera can look around on every axis. Through programming I can’t find a way to make the player model follow the camera on the y-axis. I made a simple line hoping it would work:

transform.rotation.y = cam.rotation.y;

“transform” is the player model “cam” is the camera. It works, but not completely. The player model follows the camera correctly when the camera is from 0 to 180 degrees, but past that it follows the camera like a mirror (going from 180 to 0 as the camera goes from 180 to 360). I am so confused :cry:

Thanks to anybody who helps.

transform.rotation is a Quaternion, and x, y and z have nothing to do with those nice and familiar angles shown in the Inspector/Rotation - these are the Euler angles.

You could try to use this:

  transform.eulerAngles = Vector3(0, cam.eulerAngles.y, 0);

The player will keep the horizontal plane, and follow the camera rotation around Y. But you may have problems because the Euler angles may show different combinations for the same rotation - like (0,180,0) or (180,0,180), for instance.

A better way to do what you want is to child the camera to the player, and attach the MouseLook.cs script to both - set the Axes field to Mouse X in the player’s MouseLook, and to Mouse Y in the camera: Mouse X will rotate the player and the camera (because it’s a player’s child) around Y, and Mouse Y will rotate the camera only in the vertical plane.

Kind of late but I think if you make an if statement checking if the value of the rotation is over a certain point, lets say, -180 then set it to 180, which is the same position but just the reversed value. Instead of those values try limiting it in random ranges like 0-360, -180 to 180