I'm coming back to Unity after a year or two of busy college and I'm having to relearn everything I've forgotten. One thing I cant seem to get right is this basic mouselook script I wrote. Here is the code:
float x;
float y;
public float sensitivity = 75.0f;
public float maxVerticalAngle = 75.0f;
public Rigidbody Character;
void Awake()
{
Vector3 euler = transform.rotation.eulerAngles;
x = euler.x;
y = euler.y;
}
// Update is called once per frame
void Update()
{
x += Input.GetAxis("Mouse Y") * (sensitivity * Time.deltaTime);
y += Input.GetAxis("Mouse X") * (sensitivity * Time.deltaTime);
x = Mathf.Clamp(x, -maxVerticalAngle, maxVerticalAngle);
//apply this just to the camera and it works
transform.rotation = Quaternion.Euler(-x, y, 0.0f);
//apply x changes to camera and y changes to parent rigidbody capsule and it no longer works correctly
transform.rotation = Quaternion.Euler(-x, 0.0f, 0.0f);
Character.transform.rotation = Quaternion.Euler(0.0f,y,0.0f);
}
Okay, now at the end is where my problem occurs. Let me explain a little: if I apply the quaternion with both x and y changes to the rotation then it works smoothly and the camera rotates on both axes. If, however, I apply the change in x to the camera to allow looking up and down, and I apply the change in y to the rigidbody capsule I have as a parent to the camera the script is attached to then I get a weird problem. The camera rotates up and down smoothly, but the capsule twitches and only rotates a few degrees. If, however, I comment out the line where I apply changes to the camera rotation then the parent capsule rotates normally now.
TL;DR Camera is child to a capsule. When applying rotational changes to the camera and rigidbody one line after the other, the camera rotates fine, but the rigidbody does not. If I comment out the camera's rotation changes then the rigidbody rotates fine again.
I think the problem may be that I'm rotating a parent object to the camera and I'm using mouse movement to rotate the rigidbody. I'm unsure though. Please help.
If you want to recreate what I've done, then child a camera to a capsule and apply the script. Add the capsule to the rigidbody in the script and comment out this:
//apply this just to the camera and it works
transform.rotation = Quaternion.Euler(-x, y, 0.0f);
Edit: Thank you for the help. If you need more info, then please ask!