- Home /
Lerp can someone explain please
okay, so I've been trying hard to get my charactercontroller to lerp between it's current position and a position some distance away in the x direction. I have it to where it works now, except that it happens instantly. I would like a smooth transition, but it seems like nothing works, and when I follow others advice I end up with error codes that are impossible to resolve. There is NOT a lot of info on this subject, and the game engine doesn't recognize algebraic functions that dictate velocity over time.
could someone please fill me in on how to do this. any information or advice is helpful.
Answer by kramcomposer · Nov 15, 2012 at 06:16 PM
You have a couple options, some not built into unity:
First, Ill explain how lerp works: Ill use Mathf.lerp for ease of understanding This is what Lerp basicly does internaly
SUDO CODE:
float Mathf.lerp(float from, float to,float percent){
differnce = to - from;
return = from + (differance*percent);
}
So in otherwords, If you feed in 0.10 or 10% Lerp will move the from 10% closer to the to; so the thing to remember is the distance it moves gets smaller and small... IE: Lerp(100,0,0.1) = 90 , Lerp(90,0,.1) = 81 , Lerp(81,0,.1) = 72.9 , etc , etc
You may want to use Vector3.movetowards or Mathf.moveTowards you put in a from, and to positions, but instead of a % along path, it will never move faster than the speed you feed into it;
finanaly if you need a different solution such as smooth out, and smooth in. You may want to check out iTween : http://itween.pixelplacement.com/index.php
Answer by kramcomposer · Nov 15, 2012 at 06:27 PM
you also said:
There is NOT a lot of info on this subject, and the game engine doesn't recognize algebraic functions that dictate velocity over time.
Also, to address this, You have to build you velocity over time around the update loop, meaning, you ether need to track time yourself using Time.time or Time.deltaTime or System.DataTime.Now
IE:
public Vector3 myWorldVelecoity = new Vector3(10,0,0); //Move along the world X at 10 units per second
public void Update(){
this.transform.Translate(myWorldVelecoity * Time.deltaTime);
// OR
this.transform.position = this.transform.position + (myWorldVelecoity * Time.deltaTime);
}
thank you, this is definitely what I needed, I can tweak it to work more appropriately but thanks for this starting point :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613083923im_/https://answers.unity.com/themes/thub/images/avi.jpg)