2D Rigidbody movement slows down closer to mouse cursor

Hi,

I recently started experimenting with Unity but I ran into some problems with moving my character around with the mouse. I can move my character just fine by holding down left click, but the closer my mouse cursor is to my player object, the slower it moves. I want my object to move at the set movement speed at all times but I cant figure out how to achieve this.

Could someone help me on the right track?

Thanks!

private Vector3 mousePosition;
private Rigidbody2D rb;
private Vector2 direction;
public float moveSpeed = 100f;

if (Input.GetMouseButton(0))
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = (mousePosition - transform.position).normalized;

rb.velocity = new Vector2(direction.x * moveSpeed, direction.y* moveSpeed);
}
else
{
rb.velocity = Vector2.zero;
}

You’re using Vector3 so are incorporating the Z difference too which can affect the normalized vector. You should try to use Vector2 and/or ensure Z is the same/zero when calculating your direction so you get it on the XY plane.

Note that you should use code-tags when posting code rather than plain-text.

thankuuu