Search Unity

Mouse look always snaps back to old rotation

Discussion in 'Scripting' started by bugzilla, Dec 7, 2017.

  1. bugzilla

    bugzilla

    Joined:
    Dec 9, 2008
    Posts:
    196
    I am running a simple mouse look script on my camera. Problem is when I have the camera LookAt a target object, once the user tries using mouse look again, the camera always snaps back to the old rotation. I have tried using offsets and subtracting the old rotational values but nothing works.

    Here is the old code:
    Code (CSharp):
    1.     void MouseLook(){
    2.         float mouseX = Input.GetAxis("Mouse X");
    3.         float mouseY = -Input.GetAxis("Mouse Y");
    4.  
    5.         currentRotation.y += mouseX * mouseSensitivity * Time.deltaTime;
    6.         currentRotation.x += mouseY * mouseSensitivity * Time.deltaTime;
    7.         //Debug.Log (currentRotation.x+" / "+currentRotation.y);
    8.  
    9.         currentRotation.x = Mathf.Clamp(currentRotation.x, -clampAngle, clampAngle);
    10.  
    11.         Quaternion localRotation = Quaternion.Euler(currentRotation.x, currentRotation.y, 0.0f);
    12.         transform.rotation= localRotation;
    13.  
    14.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Where's the code where you have the camera look at a target object? Are you sure you're not just continuing to run the look at code while you're using mouse look?
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm half guessing here for your goal/design, but what if you update the current rotation values when you do your "Look At"s. ?
     
  4. bugzilla

    bugzilla

    Joined:
    Dec 9, 2008
    Posts:
    196
    I am just using the standard "transform.LookAt(target object.trasnform)" to get the camera to look at the target object. I only call it once. I am absolutely sure that is not the issue as the rotation of the camera is working fine. The only issue is when I first press down the mouse button to start dragging the view always snaps back to the old rotation no matter what. I have recorded the rotation values and found that, indeed, they are the old rotational values from before I had the camera look at the target object.
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    currentRotation appears to be a "global", for the script. At the end of the script, the transform is changed, but if currentRotation held it's value, it would snap back. After the lookat, you need to change the value of currentRotation to the transform rotation. ie
    currentRotation = transform.rotation;
     
    Last edited: Dec 8, 2017
  6. bugzilla

    bugzilla

    Joined:
    Dec 9, 2008
    Posts:
    196
    That did it! In case anyone else runs into this problem, I just put this code in when the user first clicks the mouse button to start dragging the view.
    Code (CSharp):
    1.         // When first clicking right mouse, set currentRotation global to the actual rotation
    2.         if (Input.GetMouseButtonDown (1)) {
    3.             currentRotation = transform.rotation.eulerAngles;
    4.  
    5.         }
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Glad you got it working.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you got it sorted out :) And that is what I had meant, too :)