I cant get my rigidbody2d game object to move to where i click the mouse. The first click works. The object travels to the mouse click and stops. But the second time (or the third time) i click the object travels in som direction but its not quite correct and it never reaches the target! What am i not getting right?
if (Input.GetMouseButtonDown (0)) {
var mouseClick = Input.mousePosition;
mouseClick.z = player.transform.position.z - Camera.main.transform.position.z;
target = Camera.main.ScreenToWorldPoint (mouseClick);
rotateToTarget = false;
travelToTarget = true;
print ("NEW TARGET: " + target);
Debug.DrawLine (Camera.main.transform.position, target, Color.red, 10f);
}
if (rotateToTarget == false && travelToTarget == true) {
var distanceToTarget = Vector3.Distance (player.transform.position, target);
if (distanceToTarget > 2) {
print ("Distance: " + distanceToTarget);
target.z = 0;
player.rigidbody2D.AddForce (target * travelSpeed);
} else {
travelToTarget = false;
print ("travelling COMPLETE");
}
}