- Home /
Camera rotation
I'm trying to make a first person camera. Right now I have the camera copy position and rotation of Character Controller component. The body rotates like that:
void HandleRotation () {
rotateVector = Vector3.zero;
rotateVector.y = Input.GetAxisRaw ("Mouse X");
rotateVector.Normalize ();
rotateVector.y *= mouseSensitivity;
characterController.transform.Rotate (rotateVector);
}
And i have the camera follow its position and rotation:
void UpdatePosition () {
cameraObject.transform.position = PlayerLookAt.transform.position;
cameraObject.transform.rotation = PlayerLookAt.transform.rotation;
}
The problem is that I want the camera to be able to rotate up so the player can look up and down. I've tried to let the body handle that but due to Character Controllers lack of rotation constraints the whole body rotates. I've also tried to let the camera itself take care of rotation, like this :
void UpdatePosition () {
cameraObject.transform.position = PlayerLookAt.transform.position;
rotateVector = Vector3.zero;
rotateVector.x = Input.GetAxisRaw ("Mouse Y");
rotateVector.Normalize ();
rotateVector.x *= mouseSensitivity;
cameraObject.transform.Rotate (rotateVector);
cameraObject.transform.rotation = PlayerLookAt.transform.rotation;
}
but it gets overridden by rotation of the body. I thought about copying only the "y" rotation from the body and leaving the rest as is but I am not sure how to do that. I cannot change the transform.rotation values by themselves, and I've tried transform.rotation.eulerAngles.Set(), but it doesn't work either.
Your answer
Follow this Question
Related Questions
need a little help with my code camera rotation 1 Answer
Camera follow player when player rotates in y-axis 1 Answer
How to rotate the camera with the player smoothly without the camera snapping back and forth? 2 Answers
third person camera 2 Answers
Camera following player gameObject,Camera follows player, with script attached. 1 Answer