I’ve got a coroutine that moves the GameObject to a preselected position and then stops, in this coroutine, I’m trying to show the distance to that position by using Debug.DrawLine(transform.position, target)
. The problem is, that the debug line doesn’t get rendered.
Heres is the coroutine code:
RigidBody2D rigidBody2D;
Vector2 target;
void Start () {
rigidBody2D = GetComponent<RigidBody2D>();
target = (Vector2)transform.position + Vecotr2.right;
StartCoroutine(MoveHandler());
}
IEnumerator MoveHandler () {
while (true) {
Vector2 position = (Vector2)transform.position;
Vector2 heading = target - position;
float distance = heading.magnitude;
if (distance < .2f || distance > 5f) yield return null;
Vector2 direction = heading / distance;
Vector2 newPos = position + (direction * speed * Time.deltaTime);
Debug.DrawLine(position, target);
rigidBody2D.MovePosition(newPos);
yield return new WaitForEndOfFrame();
}
}