(SOLVED) Camera reset rotation then mouse click issue

Can anyone please tell me what I did wrong in my code?

I am having a strange thing happen when I reset my camera rotation using a button.

The camera resets to the correct position and rotation when the reset button is clicked, but when I click with the right mouse on the screen the camera jumps back to its previous rotation.

I do not want that to happen. I do use the right mouse button down to rotate the camera, but it can jump 180 degrees if that was its previous rotation.

I can’t seem to figure out how it pops back to its old rotation.
Here is the code I am using to move my camera and reset it.

Solution
The problem is that I was not reseting the values of:

rotationX = 0;
rotationY = 0;

in when I reset the camera!

UPDATED CODE:

using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// Use the mouse wheel to zoom in and out


[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour
{

    // target change
    public Transform target;
    public bool changeTarget;

    // reset camera
    public bool resetCam;
    private Vector3 resetPosition;
    private Quaternion resetRotation;

    // rotate
    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 10F;
    public float sensitivityY = 10F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -60F;
    public float maximumY = 60F;

    public float rotationX = 0F;
    float rotationY = 0F;
    Quaternion originalRotation;
    public float positionDamping = 5.0f;

    //zoom
    public float maxDistance = 200;
    public float minDistance = .6f;
    public float desiredDistance;
    public int zoomRate = 80;
    public float zoomInDistance = 15.0f;

    // pan 
    private float panSpeed = 0.001f;
    private float currentPanSpeed = 0.0f;

    public bool dropListOn = false;
    public bool optonsOn;               // disable mouse movements for options menu   


    public bool guiActive;

    void Start()
    {
        desiredDistance = 60;
        resetPosition = GameObject.Find("Camera").transform.localPosition;
        resetRotation = transform.localRotation;

        if (target)
        {
            transform.localPosition = target.localPosition;
        }
        else
            target.localPosition = transform.localPosition;

        originalRotation = transform.localRotation;
        currentPanSpeed = panSpeed;
        guiActive = false;
        optonsOn = false;
    }

    void Update()
    {

        // if mouse clicked on selectable target
        if (changeTarget)
        {
            UpdateTarget();
        }
        // if reset button pushed
        if (resetCam || Input.GetMouseButton(2))
        {   resetCam = false;
            [color=orange]
            rotationX = 0;
            rotationY = 0;
            [/color]
            ResetCamera();
        }

        // while mouse button down - rotate camera around target
        RotateCamera();

        // while mouse 1 and 2 buttons are down - pan the camera
        PanCamera();

        // zoom camera with mouse wheel
        Zoom();
    }

    private void Zoom()
    {
        //zoom
        // affect the desired Zoom distance if we roll the scroll wheel
        if (!dropListOn)
        {
            desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
        }

        if (Input.GetKey(KeyCode.W))
        {
            desiredDistance -= Time.deltaTime * (zoomRate / 4) * Mathf.Abs(desiredDistance);
        }
        if (Input.GetKey(KeyCode.S))
        {
            desiredDistance += Time.deltaTime * (zoomRate / 4) * Mathf.Abs(desiredDistance);
        }

        //clamp the zoom min/max
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        // For smoothing of the zoom, lerp distance
        Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, desiredDistance, Time.deltaTime * positionDamping);


    }

    private void RotateCamera()
    {
        if (Input.GetAxis("Fire1") == 1  Input.GetAxis("Fire2") != 1 || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)  !optonsOn)
        {
            if (axes == RotationAxes.MouseXAndY)
            {
                // Read the mouse input axis
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                if (Input.GetKey(KeyCode.A))
                {
                    rotationX += 1 * sensitivityX / 2;

                }
                if (Input.GetKey(KeyCode.D))
                {
                    rotationX -= 1 * sensitivityX / 2;
                }

                rotationX = ClampAngle(rotationX, minimumX, maximumX);
                rotationY = ClampAngle(rotationY, minimumY, maximumY);


                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);

                transform.localRotation = originalRotation * xQuaternion * yQuaternion;

            }
            else if (axes == RotationAxes.MouseX)
            {
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationX = ClampAngle(rotationX, minimumX, maximumX);

                Debug.Log(rotationX);

                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                transform.localRotation = originalRotation * xQuaternion;
            }
            else
            {
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = ClampAngle(rotationY, minimumY, maximumY);

                Debug.Log(rotationY);

                Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
                transform.localRotation = originalRotation * yQuaternion;
            }
        }
        // if (Input.GetKey(KeyCode.A))
        //{
        //    rotationX += 0.1f * sensitivityX;
        //    rotationX = ClampAngle(rotationX, minimumX, maximumX);

        //     Debug.Log(rotationX);

        //     Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
        //    transform.localRotation = originalRotation * xQuaternion;
        //}
        // if (Input.GetKey(KeyCode.D))
        //{
        //    rotationY += 0.1f * sensitivityY;
        //    rotationY = ClampAngle(rotationY, minimumY, maximumY);

        //     Debug.Log(rotationY);

        //    Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
        //    transform.localRotation = originalRotation * yQuaternion;
        //}
    }


    private void PanCamera()
    {
        if (Input.GetAxis("Fire2") == 1  Input.GetAxis("Fire1") != 1)
        {
            transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * currentPanSpeed);
            transform.Translate(transform.up * -Input.GetAxis("Mouse Y") * currentPanSpeed, Space.World);
        }   
        // adjust speed of Pan by distance to object
        float range = desiredDistance / 6;
        currentPanSpeed = range * panSpeed;
    }

    public void ResetCamera()
    {
        transform.localPosition = resetPosition;
        transform.position = resetPosition;

        transform.localRotation = resetRotation;
        transform.rotation = resetRotation;

        desiredDistance = maxDistance;
    }

    public void UpdateTarget()
    {
        Debug.Log(target.name + " "  + transform.name);
        transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * positionDamping);
        if (transform.position == target.position)
        {
            changeTarget = false;
        }
        desiredDistance = zoomInDistance;

    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}

No one has a clue for me yet? :frowning:

The part that looks most suspicious is this:-

if (resetCam || Input.GetMouseButton(2)) 
{
    resetCam = false; 
    ResetCamera(); 
}

I would recommend putting a print statement in here to check if the ResetCamera function is called when you click the right mouse button. This is usually button 1, rather than button 2, but there may be some problem with this. Generally the left and right buttons are fine, but we have had reports of middle buttons being reported incorrectly in some cases.

Thanks for the reply andeeee!

The problem is not that the reset is not happening; it does reset the camera rotation to its starting rotation just fine.

The problem is that after the cameras rotation has been reset, if you click on the window with the right mouse button, the camera reverts back to the rotation it had before the reset was called.

rotationX and rotationY are never set to zero for the reset.