Search Unity

MouseLook makes camera jump on first activation

Discussion in 'Scripting' started by Ronin-Ra, Nov 2, 2012.

  1. Ronin-Ra

    Ronin-Ra

    Joined:
    Jul 7, 2012
    Posts:
    42
    Edit:
    SOLVED!
    You can find the updated script on the original thread


    I'm using this nice camera movement script: http://forum.unity3d.com/threads/73117-A-Free-Simple-Smooth-Mouselook and it works perfect with the smooth movements and all.
    But when I add an activation on RMB click check in Update, something weird is happening.

    When I click RMB in the game, the camera with the script attached to it jumps from it's current rotation to 0,0,0 before starting the MouseLook instead of rotating from the cameras initial rotation.

    I'm not that well-versed in the world of camera rotation, so any help would be very appreciated.

    Here's the above mentioned script for easy access:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. // Very simple smooth mouselook modifier for the MainCamera in Unity
    5. // by Francis R. Griffiths-Keam - www.runningdimensions.com
    6.  
    7. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
    8.  
    9. public class SimpleSmoothMouseLook : MonoBehaviour {
    10.     public bool lockCursor;
    11.     public Vector2 sensitivity = new Vector2(2, 2);
    12.     public Vector2 clampInDegrees = new Vector2(360, 180);
    13.     public Vector2 smoothing = new Vector2(3, 3);
    14.     private Vector2 smoothMouse;
    15.     private Vector2 mouseAbsolute;
    16.  
    17.  
    18.  
    19.     void Update() {
    20.         if (Input.GetMouseButton(1)) {
    21.             // Ensure the cursor is always locked when set
    22.             Screen.lockCursor = lockCursor;
    23.  
    24.             // Get raw mouse input for a cleaner reading on more sensitive mice.
    25.             var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    26.  
    27.             // Scale input against the sensitivity setting and multiply that against the smoothing value.
    28.             mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
    29.  
    30.             // Interpolate mouse movement over time to apply smoothing delta.
    31.             smoothMouse.x = Mathf.Lerp(smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    32.             smoothMouse.y = Mathf.Lerp(smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
    33.  
    34.             // Find the absolute mouse movement value from point zero.
    35.             mouseAbsolute += smoothMouse;
    36.  
    37.             // Clamp and apply the local x value first, so as not to be affected by world transforms.
    38.             if (clampInDegrees.x < 360)
    39.                 mouseAbsolute.x = Mathf.Clamp(mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
    40.  
    41.             var xRotation = Quaternion.AngleAxis(-mouseAbsolute.y, Vector3.right);
    42.             transform.localRotation = xRotation;
    43.  
    44.             // Then clamp and apply the global y value.
    45.             if (clampInDegrees.y < 360)
    46.                 mouseAbsolute.y = Mathf.Clamp(mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
    47.  
    48.             var yRotation = Quaternion.AngleAxis(mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
    49.             transform.localRotation *= yRotation;
    50.         }
    51.     }
    52. }
    53.  
     
    Last edited: Nov 8, 2012
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    smoothMouse starts at zero (null?) and then you add the delta to it. So the first time the user hits the button, the camera jerks into position.
     
  3. Ronin-Ra

    Ronin-Ra

    Joined:
    Jul 7, 2012
    Posts:
    42
    Ah right, how did I miss that one :p

    But I still can't get it right... I'm very green when it comes to mouse rotations.

    Any suggestions on what I can do so the script will start from the cameras initial rotation instead?
    I tried setting smoothMouse to the initial Camera rotation, but that still jerked it into position and keep rotating the camera without me doing anything :/
     
    Last edited: Nov 2, 2012
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853

    Try using a wantedRotation and currentRotation variable to store where you are looking and where you want to look. Then use a Quaternion.RotateTowards to specify how much you are turning towards your wanted rotation and the max turn angle per frame. This should give you fine control over transitions.


    HTH
     
  5. FatiguedArtist

    FatiguedArtist

    Joined:
    Mar 3, 2010
    Posts:
    38
    I've updated the script on the original forum thread to support initial orientation, thanks for the feedback :)