How do I stop the mouse cursor from moving in a certain area?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JordenTacozTM

I’m very new to Godot and am making a little prototype where you can pick up and throw a character using the mouse. I couldn’t find a way to stop the cursor if it tries to enter an Area2D. Does anyone know how I could achieve that?

Please don’t post the same question multiple times. It only clutters the forum and (potentially) divides resources unnecessarily.

jgodfrey | 2022-08-15 17:07

:bust_in_silhouette: Reply From: jgodfrey

You can probably accomplish this via warp_mouse() (docs here), but I’m not sure I follow the mechanic you’re after.

However, generally speaking, I’d say it’s a bad idea to override the user’s mouse as it can become a frustrating experience.

What I want is to stop the mouse from moving at specific parts, so you need to throw the character from where you are and can’t just drag it all the way to the finish line. This idea won’t work if I can’t limit the mouse.

JordenTacozTM | 2022-08-15 21:14

Can you not just limit the motion of the character itself instead of limiting the mouse cursor? That’d mean that once the character has reached its position limit, the mouse cursor would continue to move, but the character would stop. Just another option. But, again, I think warp_mouse() is what you need to do what you’re asking about.

jgodfrey | 2022-08-15 21:20

I literally tried that just before you suggested. I couldn’t figure it out, but that’s on me. I tried warp_mouse already, and it didn’t work. setting the mouse position to itself gives some unexpected results, and so does mouse position - last mouse speed

JordenTacozTM | 2022-08-15 21:34

I literally tried that just before you suggested. I couldn’t figure it out, but that’s on me.

You don’t say how you’re moving your character (or what node type it is), but here’s an example with a Node2D and a child Sprite.

extends Node2D

func _process(delta):
	position = get_global_mouse_position()
	if position.x > 200: position.x = 200

If you attach the above to the Node2D, that’ll move the node (and its sprite) around with the mouse cursor. However, if you attempt to move the node past X=200, it will simply stay at X=200 (for example).

jgodfrey | 2022-08-15 22:05

I am using an area2d to block the cursor’s access, and I want to be able to use multiple of them in the level. For that reason I can’t have the positions predecided like that, as the position is not always the same, and the zone is a square. It needs to be blocked from all around

JordenTacozTM | 2022-08-15 22:15

Sure, that’s just a simple example to show how it could work. So, instead of blocking based on a pre-defined coordinate value, you want to block based on the new position value being inside some rectangle. Still, it’s the same basic principle.

jgodfrey | 2022-08-15 22:27

Didn’t work. It either teleports you to the other side or gets you stuck in the middle

JordenTacozTM | 2022-08-16 09:29