Quaternion - Euler Angles, weird variable swapping when converting

Hi everyone. I’m making this android game and I want to move my player camera by dragging across the screen. I got a really useful script for it, but I’m having a little problem that bugs me and I can’t solve. I’ve been looking for hours for the solution, but I can’t find it.
So, here’s the deal: I get the difference between my initial mouse position when I click (or tap) the screen and then I get the difference to the new mouse position. After that, I apply the rotation. Everything works fine, BUT, if I click (and hold) on the same spot twice, the camera jumps to a new rotation the first time and backwards the second time. I checked the rotation Quaternion and the X and Y variables swap, making this weird effect. I don’t know why it is happening, so If any of you can shed some light on the matter I’d be really thankful.

CODE:

    private bool bDragging;
    private Vector3 oldPos;
    private Vector3 panOrigin;
    public float panSpeed;
    public Vector3 pos;
    public Quaternion auxQuaternion;


    private float clickTimerDefault = 0.5f;
    private float clickTimer;

    private Camera firstPCamera;

    new void Start()
    {
        base.Start();
        firstPCamera = GameObject.Find("FirstPersonCamera").GetComponent<Camera>();
    }
    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            bDragging = true;
            oldPos = firstPCamera.transform.rotation.eulerAngles;
            panOrigin = firstPCamera.ScreenToViewportPoint(Input.mousePosition);                    //Get the ScreenVector the mouse clicked
            clickTimer = clickTimerDefault;
        }
        if (bDragging && clickTimer > 0)
        {
            clickTimer -= Time.deltaTime;
        }

        if (Input.GetMouseButton(0) && clickTimer <= 0)
        {
            pos = firstPCamera.ScreenToViewportPoint(Input.mousePosition) - panOrigin;    //Get the difference between where the mouse clicked and where it moved
            Vector3 aux = oldPos + pos * panSpeed;
            aux = new Vector3(-aux.y, -aux.x, 0); //Thi conversion was needed to adjust the axis
            auxQuaternion = Quaternion.Euler(aux);
            firstPCamera.transform.rotation = auxQuaternion;
        }

        if (Input.GetMouseButtonUp(0))
        {
            bDragging = false;
        }
    }

First click
105364-screenshot-156.png

Second click
105366-screenshot-157.png

Please somebody help me :c

Thanks.

You ignore the z rotation which might be the cause of your problem. Keep in mind that eulerangles is not a vector in the sense of a direction. It’s just a collection of 3 seperate angles. Quaternion to eulerangles conversion is not unique. You can simply flip all 3 angles by 180° and get the same rotation. Imagine you look just forward. If you rotate 180° on y you look in the opposite direction. If you now rotate 180° around x you doing a half looping so you look again forward but your view is upside down. Finally rotate by 180° around z and you get the same rotation as in the beginning.

If you only control your camera with this mechanic, there’s no need to read back the current rotation. Just save your last used eulerangles

I solved it, I was converting the wrong vector. Solution:

        if (Input.GetMouseButton(0) && clickTimer <= 0)
        {
            pos = firstPCamera.ScreenToViewportPoint(Input.mousePosition) - panOrigin;    //Get the difference between where the mouse clicked and where it moved
            Vector3 aux = new Vector3(-pos.y, -pos.x, 0); //This conversion was needed to adjust the axis
            aux = oldPos + aux * panSpeed;
            auxQuaternion = Quaternion.Euler(aux);
            firstPCamera.transform.rotation = auxQuaternion;
        }