I need help in coding this feature to my 2D top down shooter please

Godot Version



Hi, I am making a top down shooter and I need help with programming this in my project.

What I want to do is restrict the mouse cursor to a radius around my player. So what I mean by this is there is a circle that surrounds my player and the cursor can move freely within this circle, but when the cursor goes to the edge of the circle the cursor does not go past the edge and stays inside the circle.

Here are two sources of the same question but for unity so that you can see visually what I am trying to say:

I am sorry if my explanation is not quite clear.

Iā€™m not going to research the methods you will need but here are some tips:

These things could happen if the mouse was moved:

  1. Check the distance between the mouse cursor and your player. If the distance is smaller than the radius, you are done.
  2. If the distance is greater get the angle from the player to the mouse.
  3. Find a vector with a length of 1 pointing in the direction of the mouse (the angle you just calculated).
  4. Multiply this vector by the maximum distance.
  5. Set the mouse to the calculated position.

I hope this helps ^^

1 Like