Reply To: How to change transform.rotation

Forum Archive How to change transform.rotation Reply To: How to change transform.rotation

#36651

@jgonzalez  :,( I feel like I am never going to be finished with these issues.  At least this issue seems fairly easy. 

With all of the previous lerping statements and while statements we have been using in the co routines, I keep running into the problem of where to put the yield return null; .  If I put it inside the while loop at the very bottom, the lerp works percectly. But if instead, I put the yield return null; at the bottom of the coroutine, outside of the while loop, the lerping does not work at all and the object teleports from it’s start position to it’s end position.  The reason I want to put the yield return null; at the bottom of the coroutine instead of at the bottom of the while, is because when I put it at the bottom of the while this causes the code to break from the coroutine and go through all of the code above it for as many cycles as the while loop is programmed to go through, which is hundreds. This is causing problems because the above code is being run when I do not want it to. I would like to have the lerp move the object from start to finish smoothly and then leave the coroutine. Do you know how to achieve this? 

 

 
IEnumerator FindFood()
{
rotateAmount = new Vector3(0, transformObj.eulerAngles.y + 90, 0);
startRot = transform.rotation;
endRot = Quaternion.Euler(rotateAmount);

while (elapsedTime < 5){
elapsedTime += Time.deltaTime;
transform.localRotation = Quaternion.Lerp(startRot, endRot, elapsedTime / 5);
yield return null;
}
elapsedTime = 0;    
}