Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mathf.SmoothDamp, damping only works "one way"

Discussion in 'Scripting' started by sakus, Apr 12, 2020.

  1. sakus

    sakus

    Joined:
    Oct 9, 2013
    Posts:
    47
    Help me understand something - am I trying to use Mathf.SmoothDamp wrong or what is going on, since the damping portion of it only seems to work when the "currentVelocity" is a positive number. If it's a negative number, then the damping doesn't work but instead currentVelocity is just instantly reset to 0 when the damping should occur.

    I've been trying to google this and only found other people saying the same thing but no definite answers.

    I can work around my problem by using Vector2.SmoothDamp which works the way I would expect it to, but now I'm just curious as to why Mathf.SmoothDamp seems to work differently and would need a different approach to use it like this.

    So, I have a simple camera rotation code here using Mathf.SmoothDamp. I have a global variable to hold the currentVelocity for SmoothDamp.
    Code (CSharp):
    1. private float _refCamAxis = 0f;
    Then I have this simple piece of code which rotates my camera using SmoothDamp based on which way the mouse is moved while holding down the middle button:
    Code (CSharp):
    1.     private void DoCameraRotation()
    2.     {
    3.         var currentCamAxis = _orbitTransposer.m_XAxis.Value;
    4.         var desiredCamAxis = currentCamAxis;
    5.  
    6.         if (Input.GetMouseButton(2))
    7.         {
    8.             var mouseXPos = Input.mousePosition.x;
    9.             var rotateBy = (_prevMouseHor < mouseXPos ? -_camRotSensitivity : (_prevMouseHor > mouseXPos ? _camRotSensitivity : 0f));
    10.             desiredCamAxis = _orbitTransposer.m_XAxis.Value + rotateBy;
    11.         }
    12.      
    13.         _orbitTransposer.m_XAxis.Value = Mathf.SmoothDamp(currentCamAxis, desiredCamAxis, ref _refCamAxis, _camRotDampTime);
    14.  
    15.         _prevMouseHor = Input.mousePosition.x;
    16.  
    17.     }
    Now, when desiredCamAxis is a bigger number than currentCamAxis, resulting in a positive _refCamAxis, the damping works - the rotation comes to a smooth stop when you let go of the button (the currentVelocity ref is gradually lowered to 0).

    But when desiredCamAxis is less than currentCamAxis, resulting in a negative _refCamAxis (i.e. the camera is being rotated to the other direction), there is no damping but instead it just comes to a full stop (currentVelocity, the _refCamAxis, resets instantly to 0) as soon as the button is released.

    Ok so if I do the same with Vector2.SmoothDamp, it works both ways:
    Code (CSharp):
    1. private Vector2 _refV = new Vector2();
    The rotation function using Vector2.SmoothDamp instead of Mathf.SmoothDamp:
    Code (CSharp):
    1.     private void DoCameraRotation()
    2.     {
    3.         var currentCamAxis = _orbitTransposer.m_XAxis.Value;
    4.         var desiredCamAxis = currentCamAxis;
    5.  
    6.         if (Input.GetMouseButton(2))
    7.         {
    8.             var mouseXPos = Input.mousePosition.x;
    9.             var rotateBy = (_prevMouseHor < mouseXPos ? -_camRotSensitivity : (_prevMouseHor > mouseXPos ? _camRotSensitivity : 0f));
    10.             desiredCamAxis = _orbitTransposer.m_XAxis.Value + rotateBy;
    11.         }
    12.      
    13.         Vector2 currentDummyVector = new Vector2(currentCamAxis, 0f);
    14.         Vector2 desiredDummyVector = new Vector2(desiredCamAxis, 0f);
    15.         _orbitTransposer.m_XAxis.Value = Vector2.SmoothDamp(currentDummyVector, desiredDummyVector, ref _refV, _camRotDampTime).x;
    16.  
    17.         _prevMouseHor = Input.mousePosition.x;
    18.     }
    19.  
    And ta-dah, suddenly it works with both a positive and a negative currentVelocity.

    So - am I trying to use Mathf.SmoothDamp wrong or does it simply just work "one way" by design? Or both? Or something else?
     
    Last edited: Apr 12, 2020
    dropthepress likes this.
  2. fablemaker

    fablemaker

    Joined:
    May 11, 2020
    Posts:
    12
    I have the exact same issue here. Mathf.SmoothDamp only works with a positive target value. (Unity v2018.3)

    Thanks for workaround suggestion tho :)
     
    dropthepress likes this.