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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with Lerp

Discussion in 'Scripting' started by zreek17, Nov 9, 2013.

  1. zreek17

    zreek17

    Joined:
    Jun 2, 2013
    Posts:
    21
    Hello everyone! I am making my runner game, and I got a problem with turns. Look:
    Code (csharp):
    1.     if(Input.GetKeyDown(KeyCode.A))
    2.              transform.position = new Vector3(Mathf.Lerp(transform.position.x, transform.position.x-5, smooth*Time.time), transform.position.y, transform.position.z);
    I want my runner to change his X-position smoothly, so I am using lerp. But the problem is that it doesn't smoothes my turns:

    if smooth = 1, it changes position very fast, like there is no smooth at all;
    but if smooth<1, it changes position very fast as well, but it is less that 5.

    So how to make a smooth change in position with Lerp?
     
  2. fenderrio

    fenderrio

    Joined:
    Mar 2, 2013
    Posts:
    147
    Lerp functions will return a linearly eased value between the from and to value that you provide. The time/progress parameter needs to be a value between 0 and 1, with 0 resulting in the from value being returned, and a 1 returning the to value, and numbers between returning the progression between the two values.

    Your code currently listens for the key-down event of the A key, and then instantly moved the transform position to x-5, since you're passing in Time.time which will likely be greater than 1.

    If you're wanting to change your players direction gradually, I'd suggest maintaining an x-velocity value and lerping it up to the desired speed given the players current running direction.

    Code (csharp):
    1.  
    2. float x_velocity = 0;
    3. float MAX_SPEED = 5;
    4. float SMOOTH_DAMPING = 0.1f;
    5.  
    6. if(Input.GetKey(KeyCode.A))
    7.      x_velocity = Mathf.Lerp(x_velocity, -MAX_SPEED, SMOOTH_DAMPING);
    8. else if(Input.GetKey(KeyCode.D))
    9.      x_velocity = Mathf.Lerp(x_velocity, MAX_SPEED, SMOOTH_DAMPING);
    10. else
    11.      x_velocity = Mathf.Lerp(x_velocity, 0, SMOOTH_DAMPING);
    12.  
    13. transform.position += new Vector3(x_velocity, 0 0);
    14.  
    Or something along those lines...
    Hope that helps.
     
    Last edited: Nov 9, 2013
  3. zreek17

    zreek17

    Joined:
    Jun 2, 2013
    Posts:
    21
    It is amazing! It is working! fenderrio, thank you very much!
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you use transform.position.x and transform.position.x-5, both those positions will change over time. You have to use a variable for the final position so it doesn't change.
     
  5. fenderrio

    fenderrio

    Joined:
    Mar 2, 2013
    Posts:
    147
    Brilliant! No problem. :)