Search Unity

Lerp Issue

Discussion in 'Scripting' started by taskodinson, Mar 27, 2015.

  1. taskodinson

    taskodinson

    Joined:
    Jul 16, 2013
    Posts:
    23
    I've recently discovered that I'm using lerp wrong. This is my current code to set the speed of my vehicle:
    Code (CSharp):
    1. targetSpeed = Slider.value;
    2. currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed,Time.deltaTime);
    I like the effect it makes, but it isn't framerate independent. To try and replicate it using the correct method I got this:
    Code (CSharp):
    1. lastFrameSpeed = targetSpeed;
    2. targetSpeed = Slider.value;
    3.  
    4. if(lastFrameSpeed != targetSpeed ) //if the slider has changed
    5. {
    6.     rate = 0;  //reset timer
    7.     startSpeed = currentSpeed;
    8. }
    9.  
    10. rate+=time.deltaTime;
    11. currentSpeed= Mathf.Lerp(startSpeed,targetSpeed ,rate);
    What this does is reset the lerp if the slider that controls the speed has changed. It works good if I go from one value to another and leave it, but if I keep changing the slider by dragging it, the lerp is continually reset and the speed stays the same.

    Is there a better way of doing this, that keeps the smooth slow down, is framerate independent and also allows the slider to be continually changed by the player? Thanks.

    Edit: Nevermind, seems like smoothdamp is what I was looking for.
     
    Last edited: Mar 27, 2015