I’m working on some top down controls, an example of this is when you press W the ship moves towards wherever your mouse is on the screen, shoots a weapon that way, etc.
I want to stop the mouse in a 20px radius around the player to stop the mouse from causing the ship to rotate randomly when the mouse reaches pos 0,0 / is on top of it.
What method would be best to clamp the mouse from moving fully on top of the character, or stop responding to the mouse location when it is there thus stopping the spinning effect.
I have attempted to use warp_mouse() but i’ve run into issues with viewport vs ship global pos, etc. Did a little digging and I’ve not really spotted a solution I have been successful at implementing yet. Figured it was time to reach out to all you helpful devs. Thank you in advance!
If this works, here is a short video of the ship flailing about when the mouse gets ontop of it:
You could add an area2d to the ship with a circle collisionshape2d centered on the ship that goes out 20 pixels. Attach the mouse_entered and mouse_exited signals to your ship’s script and toggle a boolean “mouse_is_too_dang_close_to_the_ship” from these signal handlers. Then condition on that bool in the ship rotation logic - if true, disable rotation.
An alternative/easier approach would be to just get the distance from the ship to the cursor and only rotate to the mouse position when the distance is big enough.
Something like this:
var mouse_pos := get_global_mouse_position()
if global_position.distance_to(mouse_pos) > 20:
look_at(mouse_pos)
This doesn’t stop the mouse but it does stop the ship from reacting.
Instead of a direct look_at() you could tween the ship’s direction changes, even give it a maximum of rotation degrees/second. Then it would flip around to hard.