Why doesnt my Y axis update?

So, I’m trying to make my object’s y-axis match my camera’s y-axis, and for some reason the Rotation component doesn’t change when my camera changes.

My VERY SIMPLIFIED code:

public class MovementControllerWalking : MonoBehaviour
{

    // Camera Entities
    public GameObject cameraObject;
    public Quaternion cameraRot;

    // Player Entities
    
    
    void Update()
    {
        cameraRot = cameraObject.transform.rotation;
        GetComponent<Transform>().rotation = new Quaternion(0.0f, cameraRot.y, 0.0f, 0.0f);
    }
}

Transform is accessible from every [Component][1] so you should not do a GetComponent<Transform>() on any object. Instead you can just call "transform". [1]: https://docs.unity3d.com/ScriptReference/Component.html

1 Answer

1

You can’t get rotation by using Quaternion’s Y. Quaternions are a lot more complex than that.

Use eulerAngles (Or localEulerAngles if camera is parented under an object)

   public Transform mainCam;

    private Vector3 cameraRot;

    private void Update()
    {
        cameraRot = mainCam.eulerAngles;

        transform.rotation = Quaternion.Euler(0f, cameraRot.y, 0f);
    }