I am fairly new to rigidbody velocity and how to get it to work properly. Currently, with the code below, my player marker will move across the screen towards where I last clicked on the screen. Once it arrives at where I last clicked it will stop. All of this works just as I had hoped. My only issue is that when I click somewhere relatively far (player marker on left side of screen and I click right side of screen) my marker moves at a desirable speed, but if I click relatively close to where my marker currently sits then I notice the velocity seems extremely low. Can anyone help me fix my code to where my marker will always move at a set velocity to its destination, no matter how far or near? Thanks!
public class PlayerMarker : MonoBehaviour
{
Rigidbody2D rig;
public float moveSpeed;
private Vector3 targetDestination;
void Start()
{
rig = GetComponent<Rigidbody2D>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
targetDestination = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Move(targetDestination);
}
float distanceFromDestination = Vector2.Distance(transform.position, targetDestination);
if(distanceFromDestination <= .01)
{
rig.velocity = Vector2.zero;
}
}
void Move(Vector3 destination)
{
Vector2 dir = (destination - transform.position).normalized;
rig.velocity = dir * moveSpeed;
}
}