Player look script gonna give me anxiety [Solved]

I have been picking at this for a while and still have no idea how or why the player practially summons satan when they look up or down. This is a script for player head rotation and it works as intended except from when the player looks far enough up or down.

I have an object with a rigidbody on it, and a camera inside this object. Quite new to CSharp and object based programming so im a bit slow in the head when it comes to getting my head around this stuff. Im suffering a bit of sunken cost fallacy so I kinda dont want to use another method for looking around.

using UnityEngine;

public class rotationManager : MonoBehaviour
{

    private float yaw, pitch;
    private Quaternion playerRotationX, headRotationY;

    //parent object to the camera
    [SerializeField] private Transform Player;

    //the camera
    [SerializeField] private Transform Head;

    void Update()
    {

        // float keeps track of the rotation of the head
        yaw += Input.GetAxis("Mouse X");
        pitch += Input.GetAxis("Mouse Y");

        // set two quaternions for each rotation
        playerRotationX = Quaternion.AngleAxis(yaw, Player.up);
        headRotationY = Quaternion.AngleAxis(-pitch, Head.right);

        // set the rotation of the player body and player head
        // multyiply the X rotation of the player as Camera x moves independantly

        Player.rotation = playerRotationX;
        Head.rotation = headRotationY * playerRotationX;

        Debug.Log(headRotationY.eulerAngles + " : " + playerRotationX.eulerAngles);
    }
}

It appears that applying either rotation on their own will result in a success however together they do not :confused:

If the head transform is a child of the player then it’s redundant to apply the X rotation to the head.

1 Like

If I don’t apply the X rotation of the parent to the head the head stays still (globally) despite the parent rotating. I am confused by this as the camera is definitley a child to the player.

Just got it working:

using UnityEngine;

public class rotationManager : MonoBehaviour
{
    private float yaw, pitch;
    private Quaternion playerRotationX, headRotationY;

    //parent object to the camera
    [SerializeField] private Transform Player;

    //the camera
    [SerializeField] private Transform Head;
   
    void Update()
    {

        // float keeps track of the rotation of the head
        yaw = Input.GetAxis("Mouse X");
        pitch = Input.GetAxis("Mouse Y");

        // set two quaternions for each rotation and multyiply the X rotation of the player as Camera x moves independantly
        playerRotationX = Quaternion.AngleAxis(yaw, Vector3.up);
        headRotationY = Quaternion.AngleAxis(pitch, Vector3.left);

        // set the rotation of the player body and player head
        Player.rotation *= playerRotationX;
        Head.rotation *= headRotationY;

    }
}

Instead of setting the rotation to the transforms of the objects I have applied them. I have NO IDEA why setting the rotation bugs the camera out but I’m happy this now functions :slight_smile:

Though you got it working, the reason this was happening was because you were setting rotation and not localRotation.

Ahh, I see now. Thank you for your help.