Hello everyone, I have a gameobject whose script has 2 coroutines for lerping. When this game object is activated, it takes object A and sets its position & rotation to object X’s position & rotation. And then, it starts a coroutine. This coroutine lerps object A to a desired position, also it lerps it’s rotation too. And when I press F key, it calls another coroutine. Which takes the object A to X’s position and rotation back. But I have a problem, first lerp happens without a problem, but the second one has problems. It starts too slow, goes to wrong position, and suddenly in a split of a second it goes to where it is supposed to go. It’s not logical, I could not understand the problem. Both coroutines have different float values in order to determine the time. I think problem is about the calculations of these. Just look at the statistics and informations above and then the code itself.
First Lerp Coroutine Time : 2 or around - It works okay, if I decrease the value it fastens which is okay.
First Lerp Coroutine Time : 5,6,7,8,9,… - It looks like after some value it does not slow down the lerping, couldn’t understand why.
Second Lerp ( lerping back to first position & rotation ) Coroutine Time : 2 or around - It works okay, but if I put anything bigger than 2-3-4 or something like that it does not work and the problem I told you above starts to occur.
Here are my lines :
IEnumerator Lerp(float time)
{
float elapsedTime = 0.0f;
while(elapsedTime < time)
{
cameraHolder.localPosition = Vector3.Lerp(cameraHolder.localPosition, referanceTransform.localPosition, elapsedTime / time);
cameraHolder.localRotation = Quaternion.Lerp(cameraHolder.localRotation, referanceTransform.localRotation, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
}
// I call the first lerp like this where FirstTime is a float StartCoroutine(Lerp(FirstTime));
IEnumerator LerpBack(float time)
{
float elapsedTime = 0.0f;
while (elapsedTime < time)
{
cameraHolder.localPosition = Vector3.Lerp(cameraHolder.localPosition, playerCamReferance.localPosition, elapsedTime / time);
cameraHolder.localRotation = Quaternion.Lerp(cameraHolder.localRotation, playerCamReferance.localRotation, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
}
// I call this one like this where SecondTime is a float StartCoroutine(LerpBack(SecondTime));
PS. I do not have anything wrong with the position I set as a parameters for lerp functions. I double checked them with debugging, nothing is wrong. When I mess with the time values, really weird results can occur in the second lerp. Any ideas ? Thanks :).
EDIT : I debugged the elapsedTime value and it reaches to the given time long after the lerp is visually finished.