I found out the problem.
Problem with Mathf.Lerp
Hello everyone, I'm trying to lerp between two values.
if(gameObject.transform.position != endPos)
{
float currentDuration = Time.time - lerpStartTime;
float journeyFraction = currentDuration / totalDistanceToDestination;
gameObject.transform.position = new Vector3(Mathf.Lerp (startPos.x, endPos.x, journeyFraction * 2f), gameObject.transform.position.y, gameObject.transform.position.z);
print("posX: " + gameObject.transform.position.x);
}
else if(gameObject.transform.position == endPos) { lerping = false; alreadySwiped = false;}
This is the code i'm using. So, the problem is, it never gets tot the end of the "endPos.x".
For a better understanding, here is the output of the print in the first if statement:
starPos = (24.3, -22.5, -11.7)
endPos = (31.9, -22.5, -11.7)
posX: 30.9175
posX: 31.33167
posX: 31.74583
posX: 31.86094
posX: 32.16732 -- Here it goes over the maximum value.
posX: 31.86094 -- Here it goes to the final value again.
posX: 32.58005 -- Again, but this time the number is bigger.
posX: 31.86094
posX: 32.99336
posX: 31.86094
posX: 33.40636 -- And it just goes on like this, and never stops.
I'm out of Ideas. I tried Vector3.Lerp too, the output is the same. I'm happy for every suggestion you may have. Thank you.
Look again at the t factor for Lerp (Its the same for all versions).
http://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Lerp.html
Then search for "Easing Lerp" and see that its the same but used slightly differently.
Easing lerps generally never reach the end and rely on a changing endpoint rather than modifying t. In this case t affects the size of the step taken.
Answer by Robxd0 · Nov 21, 2015 at 04:13 PM
Oh, thanks that was useful. However, I found out why it was bouncing between that two values at the end. I had accidentally implemented the script twice on the same object... Awkward mistake, but thank you anyway :).
Follow this Question
Related Questions
Coroutine: delay transform rotation but start transform movement immediately 2 Answers
Movement and Direction script bugging out 1 Answer
If statement not getting called after Lerping a Scale 1 Answer
Lerping Camera Between Two Points 0 Answers
Object moving chaotically using lerp and repeating. 1 Answer