I have a coroutine that is initiated when the user enters a trigger and then presses a specific key. The rotating as a whole works, but towards the end of the rotation it begins to rotate much, much slower. I have had this issue when using Vector3.Lerp but I fixed that by using speed * Time.deltaTime and another variable to make it smooth. I tried to use a step as per the Quaternion.RotateTowards unity docs but it would just not rotate at all.
void Update()
{
if (Input.GetKey(KeyCode.Z))
{
Debug.Log("Raycast.");
if (Physics.Raycast(transform.position, Vector3.down, 1.5f))
{
StartCoroutine(Turn(cubeObject));
}
}
}
IEnumerator Turn(Transform cubeObject)
{
Quaternion targetRotation = Quaternion.identity;
do
{
Debug.Log("Begin rotating.");
Vector3 targetDirection = cubeObject.position - transform.position;
targetRotation = Quaternion.LookRotation(targetDirection);
Quaternion nextRotation = Quaternion.RotateTowards(
transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
transform.localEulerAngles = new Vector3(0, nextRotation.eulerAngles.y, 0);
yield return null;
} while (Quaternion.Angle(transform.rotation, targetRotation) == 0f);
}
And if I change the very last value on the last line (Quaternion.Angle) to anything greater than 0, the rotation never ends. I will be locked in rotation until I exit runtime. I'm only trying to rotate fully towards the target just once when the appropriate key is pressed.