Camera teleporting to old rotation

So I have this weird issue with my camera where if I look around after moving my player to a new location it causes the camera to revert back to the original look rotation before I started moving. I’m guessing Quaternion.Euler might be an issue?

    public void MoveCamera()
    {
            xDeg += Input.GetAxis("Mouse X") * 100 * 0.02f;
            yDeg -= Input.GetAxis("Mouse Y") * 100 * 0.02f;
            yDeg = Mathf.Clamp(yDeg, -60, 80);

            Quaternion rotation = Quaternion.Euler(yDeg, xDeg, 0);
            Vector3 vTargetOffset = new Vector3(0, -1.2f, 0);

            this.transform.rotation = rotation;
    }

Here’s my newer camera code which fixed the above code issue but the clamp is not working. The above code clamping the camera rotation is working.

    public void MoveCamera()
    {
            float pitch = Mathf.Clamp(Input.GetAxis("Mouse Y") * 100, -60, 80);
            float yaw = Input.GetAxis("Mouse X") * 100 * Time.deltaTime;

            transform.Rotate(-pitch * Time.deltaTime, 0, 0, Space.Self); 
            transform.Rotate(0, yaw, 0, Space.World);
    }

The newer function that can’t clamp does clamp but is irrelevant to what you want. The way Transform.Rotate() works is it applies the rotation given onto the transform’s current rotation but doesn’t set the rotation of the given transform. This means, you’ll need one last line of code, which is:


transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x, Mathf.Clamp(transform.rotation.y, -60, 80), transform.rotation.z));

This clamps the transform’s y rotation and sets it in the process.

@jessee03