- Home /
Lerp to position, after a while come back, problem occured using Coroutines
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.
Answer by Patel-Sagar · Feb 05, 2015 at 07:56 AM
Hey Inan,
If you want your cameraHolder to reach at destination in exact time, then you have to pass 'start localPosition' in Lerp function not 'cameraHolder.localPosition'.
so your code would be like,
IEnumerator Lerp(float time)
{
float elapsedTime = 0.0f;
Vector3 startLocalPosition = cameraHolder.localPosition;
Vector3 startLocalRotation = cameraHolder.localRotation;
while(elapsedTime < time)
{
cameraHolder.localPosition = Vector3.Lerp(startLocalPosition, referanceTransform.localPosition, elapsedTime / time);
cameraHolder.localRotation = Quaternion.Lerp(startLocalRotation, referanceTransform.localRotation, elapsedTime / time);
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
}
and also you can do the same for second Ienumerator. I have written startLocalPosition for your understanding. Maybe starting position is already saved with you in playerCamReferance.localPosition. if that is the case then replace that with startLocalPosition in my code. Let me know if you have any query.
Regards.
Oh so that was the problem, because it is in while loop and coroutine, I have to store the first position and rotation in Vector3 and Quaternion, alright, thanks !
Your answer
![](https://koobas.hobune.stream/wayback/20220613182533im_/https://answers.unity.com/themes/thub/images/avi.jpg)