Lerp with Coroutine doesn't behave as expected
A few Important notes:
Lerp is not used in an Update() function. For specific reasons I am using a Coroutine.
The goal is to move a transform from Point A (its current position) to point B. (which its location is random, for now, lets say its higher than point A.)
After transform has arrived, an injected function is called.
This Lerp is the last one out of a series of Lerps triggering one after the other.
The Bug:
The Transform always arrives to Point B as expected. The function injected is not called 99% of the time, for an unknown reason. The weird thing is that sometimes it does actually call the function.
Things I Checked:
Function is never null, it's always assigned correctly.
I Tried multiple values for each of the params to see if there is a common behavior, there isn't. In some tries the function is called, in most of the tries it doesn't.
I tried braking the While() loop if the transform reaches point B before the given duration, same results.
The Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; public static class LerpUtil { public static IEnumerator LerpPosition(Transform obj, Vector3 startPosition, Vector3 endPosition, float duration, OnAnimationFinish onAnimationFinish = null) { float time = 0; while (time < duration) { obj.position = Vector3.Lerp(startPosition, endPosition, Mathf.SmoothStep(0, 1, time / duration)); time += Time.deltaTime; yield return null; } obj.position = endPosition; if (onAnimationFinish != null) { onAnimationFinish(); } } }
Your answer
Follow this Question
Related Questions
Crouch Lerp Coroutine Problems 0 Answers
How do I achieve "inertial" camera movement for mouse look? 1 Answer
Lerp Doesn't Work On Mobile 1 Answer
Object rotation with angle constraints 0 Answers
Why my angle lerp code take only 30% time to finish? 0 Answers