- Home /
Lerping Issue
I've implemented the lerping method to smoothly transition the position of a transform from one position to another, to have that "decelerating" effect.
From my understanding, the way this is accomplished is by moving a certain percentage (the "t" parameter) of the distance between the two positions on every update, then updating the initial position (the "a" parameter) to the position moved to, and repeating. This way, on every update, the transform moves closer to its final position by a smaller amount, the "speed" of movement appears to be decreasing, and thus the smooth decelerating effect.
I have an object that calls a coroutine that carries out this transition up into my view. Immediately after the subroutine call, the script checks for input. If a there is user input, a 2nd function is called on every update to carry that same object downwards out of the scene at a certain speed.
Here's my problem: If the user inputs before the coroutine finishes executing, and enables the 2nd function, then the coroutine never finishes executing. This is because the coroutine is applying a upwards, positive speed and the 2nd function is applying a downwards, negative speed, so the object becomes stationary. This is not what I want.
Now, I could wait until the coroutine finishes before checking for user input, BUT, because of the method I'm using (described in the second paragraph), there's a point in time where the transform appears to have reached its destination, or is close enough for the user to have understood its use and attempted to interact with it, but is actually still lerping; still moving by some very small amount. At this point the user would probably want to interact with the game, but wouldn't be able to if the game is waiting for the subroutine to finish. This is the case even if I stop the lerping when the distance between the transform's position and the final position is a very tiny amount; the deceleration time isn't predictable in this way.
So, a potential solution I found is to use lerp based on a sine function, as this could provide that same decelerating effect. The problem with this is that a call to the sine function is more expensive than just using a fixed value for the t parameter of the lerp... I'm not sure how much more expensive, but it's definitely more predictable.
Is there any other solution to this? Is this method of lerping just not appropriate for these kinds of UI dependent transitions? Thank you!
Answer by ahstein · Jun 26, 2018 at 08:25 PM
If the user interacts with the object, can you just set some flag to stop the lerp early?
Tried that, but I'm afraid of the user accidentally interacting with the object without the lerp being fully completed... The action that I'm actually looking for is the user tapping the screen, so that the 2nd method I mentioned is called repeatedly. If the user accidentally taps before the lerp is completed, then the 2nd method will start updating the object while it's in a position it's not intended to be in.... does that make sense?
Your answer
Follow this Question
Related Questions
why one works, the other does not 2 Answers
Child GameObject is aligned to world axis and not the parents axis (UPDATE) 1 Answer
Camera Controller to Player Position Conflict 1 Answer
Unity smooth following 2D camera not working properly on different resolutions 0 Answers
Getting Vector3 in between two given Vector3 after given Distance. 2 Answers