- Home /
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 :'(
Thanks to anybody who helps.
Answer by aldonaletto · Feb 26, 2012 at 11:33 PM
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.
Well i've tried the child thing you said and the camera and model are following each other as it should.
But when i'm walking forward and then look to the left or right, it suddenly looks like i'm rotating around some point behind me, looks weird and i would really like a solution on that if possible, though my biggest problem is than when i walk forward and look around, i suddenly fall through the terrain.
I've got colliders on and there's not a single problem until i start rotating.
Any solutions?
Answer by Josh707 · Nov 22, 2012 at 12:24 AM
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
Your answer
Follow this Question
Related Questions
Camera not rotate when following player 1 Answer
How to rotate the player to the direction of the camera 1 Answer
How can I keep a player inside the camera box? [C#] 3 Answers
How to set the player in front of the camera in any rotation 1 Answer
Rotating GameObject Around Player With "Mouse Y" Position 2 Answers