I am trying to rotate a 3D gameobject towards a 3D Cylinder using this function:
IEnumerator RotateTowardsCylinder()
{
bool waiting2Seconds = false;
Vector3 targetRotate = new Vector3(cylinder.position.x - transform.position.x, cylinder.position.y - transform.position.y, cylinder.position.z- transform.position.z);
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetRotate, 0.01f, 0);
transform.rotation = Quaternion.LookRotation(newDirection, transform.up);
while (targetRotate.normalized.x != newDirection.normalized.x || targetRotate.normalized.z != newDirection.normalized.z)
{
transform.rotation = Quaternion.LookRotation(newDirection, transform.up);
targetRotate = new Vector3(cylinder.position.x - transform.position.x, cylinder.position.y - transform.position.y, cylinder.position.z - transform.position.z);
newDirection = Vector3.RotateTowards(transform.forward, targetRotate, 0.01f, 0);
Debug.Log("targetRotate " + targetRotate.normalized + "newDirection " + newDirection.normalized);
yield return null;
}
waiting2Seconds = true;
Debug.Log("waiting 2 sec " + waiting2Seconds);
yield return new WaitForSeconds(2.0f);
StartCoroutine(MoveTowardsCylinder());
moveFunctionRuns = true;
Debug.Log("MoveTowardsCylinder " + moveFunctionRuns);
}
It doesn’t seem to ever go out of the while loop, because I dont see my Debug.Log messages appearing in the console. Why is this? Please help, I would be grateful!