Search Unity

Camera snap back issue

Discussion in 'Scripting' started by jessee03, Dec 22, 2020.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    So I have my camera set up so it can only look around when the player is standing in place. Right now it's a click to move system. The problem I'm running into is the camera is snapping back to a previous rotation if the player tries to move the camera around after reaching a new location.

    Example:
    -look around with camera
    -click to move to a new position
    -reach the new position
    -look around with camera again ( rotation of camera immediately snaps back to the old rotation that the camera was set to before walking to the new location )

    Code (CSharp):
    1.     public void MoveCamera()
    2.     {
    3.             xDeg += Input.GetAxis("Mouse X") * 100 * 0.02f;
    4.             yDeg -= Input.GetAxis("Mouse Y") * 100 * 0.02f;
    5.             yDeg = Mathf.Clamp(yDeg, -60, 80);
    6.  
    7.             Quaternion rotation = Quaternion.Euler(yDeg, xDeg, 0);
    8.             Vector3 vTargetOffset = new Vector3(0, -1.2f, 0);
    9.  
    10.             this.transform.rotation = rotation;
    11.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm guessing it is because when you call MoveCamera() you're using xDeg and yDeg, which are defined somewhere else, and the value of which carry over between calls to this method. You haven't posted hardly any of the code for all the stuff you mention in your Example though.