- Home /
Mathf.Lerp happens instantly
Hello,
I have this script that it's supposed to move the player 1 unit to the right smothly when I press Space. The problem is that he gets moved instantly, like teleporting. What's wrong?
//clickPos is the position the time we click to move the player
var clickPos : int = don.transform.position.x;
don.transform.position.x = Mathf.Lerp(clickPos, clickPos + 1, 0.5);
Answer by Eric5h5 · Dec 09, 2014 at 07:49 PM
Lerp is just a very simple math function that returns a value instantly, like most any other function. If you want to use Lerp to do stuff smoothly, you need to call it repeatedly (in a coroutine typically, or Update) while advancing the third parameter from 0.0 to 1.0.
Hmm.... How can I do this? I can't use Time.time because I can't zero it back
Answer by f4bo · Dec 09, 2014 at 08:15 PM
that is not how Mathf.Lerp is supposed to work. The rightmost parameter tells the function how much add to clickpos but it is not cumulative, it is meant always starting from the base value, clickpos in the specific. So your instruction is actually telling to go, each time is called, from clickpos to clickpos+0.5, therefore going instantly halfway the x position you would reach. To achieve what you need you have to change that 0.5 with, i.e., something that will increment by the time, like: (Time.time - startTime) speed where startTime initialized with Time.time before your instruction starts looping, and speed* a value used to control the Lerp speed to complete the gap between the starting value and the value to reach.
Your answer
Follow this Question
Related Questions
Trying to lerp in different states in a switch statement 0 Answers
Controlling duration of Color.Lerp in seconds 2 Answers
how to use time on lerp 2 Answers
How you change another script value smoothly on trigger 0 Answers
How do I make this lerp? 1 Answer