Search Unity

Object stops moving foward when lerping

Discussion in 'Scripting' started by Spaguetti, Oct 20, 2018.

  1. Spaguetti

    Spaguetti

    Joined:
    Oct 20, 2018
    Posts:
    4
    I am making an infinite runner, but when I try lerping between lanes, the character stops moving fowards, do the lerp and then keeps going. How can I make it lerp while still moving? Sorry about some var names, when I started coding I used to use my native language, but I stopped doing that and I'm still changing their names to english.



    Code (CSharp):
    1.  void DoLerpLeft()
    2.     {
    3.      
    4.         isLerping = true;
    5.         timeLerpStarted = Time.time;
    6.  
    7.         startPos = personagem.transform.position;
    8.         finishPos = personagem.transform.position + Vector3.left * lerpDistance;
    9.  
    10.     }

    Code (CSharp):
    1. void Movimenta()
    2.     {
    3.         Vector3 movimenta;
    4.  
    5.         movimenta = new Vector3(0, 0, personagem.velocidade);
    6.  
    7.         personagem.transform.position = personagem.transform.position + movimenta;
    8.     }
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         Movimenta();
    4.  
    5.         if (isLerping)
    6.         {
    7.             float timeSinceStarted = Time.time - timeLerpStarted;
    8.             float lerpPercentage = timeSinceStarted / changeLanesTime;
    9.  
    10.  
    11.             personagem.transform.position = Vector3.Lerp(startPos, finishPos, lerpPercentage);
    12.  
    13.             if (lerpPercentage >= 1.0f) isLerping = false;
    14.         }
    15.     }