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);
}
}