I’m having trouble with a script that moves an object to a specific vector. The following method is a couroutine that is passed the target’s position. The execution hangs up at the point where the Debug action is called.
Is this an error having to do with floating point comparisons? Any help is appreciated, thanks in advance.
IEnumerator GoTo (Vector3 target)
{
bool walking = true;
while (walking == true)
{
float targetDistance = (Vector3.Distance (transform.position, target));
Vector3 targetLocation = new Vector3 (target.x, transform.position.y, target.z);
if (targetDistance >= 3.0f)
{
MoveTo (targetLocation);
RotateTo (targetLocation);
Debug.Log (targetDistance);
}
else
{
while (targetLocation - transform.position != Vector3.zero && transform.rotation != Quaternion.LookRotation (targetLocation - transform.position))
{
RotateTo (targetLocation);
yield return 0;
}
TargetReached (gameObject);
walking = false;
}
yield return 0;
}
}
void MoveTo (Vector3 targetLocation)
{
transform.position = Vector3.MoveTowards (transform.position, targetLocation, Time.deltaTime * myParams.speed);
}
void RotateTo (Vector3 targetLocation)
{
if (targetLocation - transform.position != Vector3.zero)
{
Vector3 turnDirection = Vector3.RotateTowards (transform.forward, targetLocation - transform.position, Time.deltaTime * myParams.turnSpeed, 1.0f);
transform.rotation = Quaternion.LookRotation (turnDirection);
}
}