• Tutorials
  • Creating boundary area for mouse movement

Hello everyone! I am developing a 2D game, and the character can aim using the mouse. I wanted a border area where, when the mouse reaches the end of the area, it did not move forward. Would anyone have any suggestions for this?

You want it where the mouse does not move forward, or the character?

For the mouse, you can use warp_mouse to teleport the mouse back inside the boarder area once it's outside of the border area. Figuring out whether the mouse is outside the border area could prove to difficult, and I currently cannot think of a great way to go about checking if the mouse is within a section/area of a scene or not. I would suggest looking into using a Rect2 and the contains_point function.


If you do not want to move the player when the mouse is at the end of the area, then it's a little easier. One easy way to get around the player movement issue is to just place a bunch of StaticBody2D nodes around the border so the player cannot move through that area.

Another way you could do it is place a Area2D node with collision shapes around the area you want the player to be able to move. Then you can connect the body_exited signal and then disable player movement when the player has exited, and then enable movement using the body_entered signal.

Good Morning! Thanks for the quick response. Yes, I needed to make the mouse not move in a certain period, and I did an "if" sequence in which, when the mouse position was larger and +100 in relation to the player, it matched this position. Good luck and if you want, you can put the topic as solved.