Freeze rotation constraints are not freezing the object

I have a very simple first person camera set up, there is a capsule with a capsule collider and a camera inside it that is a child of the capsule. I have a scripts that makes the camera rotate up and down and the capsule turn left and right which allows you to look in every direction. The capsule has it’s X and Y rotations frozen in the rigid body component. When I look in a certain direction the capsule starts to precess around the Y axis quite violently.

Here is the code from my script (I am aware it’s not a good camera I’m only messing around to find out what everything does at the moment):

public class Camera : MonoBehaviour {

public float horizontalSpeed = 10.0F;
public float verticalSpeed = 10.0F;
float v;
float h;
Vector2 mouseMovement;
public GameObject capsule;

void Start () {
	Cursor.lockState = CursorLockMode.Locked;
	capsule = this.transform.parent.gameObject;
}

void Update () {
	h = horizontalSpeed * Input.GetAxis("Mouse X") * Time.deltaTime;
	v = verticalSpeed * Input.GetAxis("Mouse Y") * Time.deltaTime;
	mouseMovement += new Vector2 (h, v);
	transform.localRotation = Quaternion.AngleAxis (-mouseMovement.y, transform.right);
	capsule.transform.localRotation = Quaternion.AngleAxis (mouseMovement.x, transform.up);

	if (Input.GetKey(KeyCode.Escape)) {
		Cursor.lockState = CursorLockMode.None;
	}
}

}

And this is the script for the movement of the capsule:

public class PlayerMovement : MonoBehaviour {

Vector3 movement;

void Start () {

}

void Update () {
	movement = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
	movement *= 10;
	movement *= Time.deltaTime;
	transform.Translate (movement);
}

}

You are moving the camera using it’s Transform, and you have locked the rotation in the RigidBody.

I suggest either applying the rotation to the RigidBody (terrible idea), or storing the rotation you want locked in a variable at the beginning of your script, and then restoring the y rotation at the end of your script.

HUGE WORD OF ADVICE: use Quaternion.eulerAngles (so transform.localRotation.eulerAngles.y for getting the y rotation) instead of just the raw Quaternion, as quaternions are weird elements of dark magic that use what I guess could be called a Vector4, and you’ll just get in a whole mess if you use the raw Quaternion.