0 votes


I'm trying to make a mouse event where i have to hold the cursor in a circle, and it's trying to push the cursor out of itself, like seen in many games. I searched a lot for mouse move function but i had no luck. Any idea about this?

in Engine by (16 points)

I'm sorry but I'm not sure if I understand which effect you want to achieve. Do you have some sample videos/gifs containing this mouse event?

As far as I know there is no set_mouse_position-function but you could hide the OS specific mouse cursor and create your own with his very own movement logic.

Maybe the warp_mouse()-function could help?

1 Answer

0 votes

The following code should do what you are asking. Attach this to your circle sprite.

extends Sprite

var pixel_remainder = Vector2(0, 0)
const RADIUS = 32 * 6

func _process(delta):
    var mouse_pos = get_viewport().get_mouse_position()
    var distance = mouse_pos.distance_to(self.position)
    if distance < RADIUS:
        pixel_remainder  += (mouse_pos - self.position).normalized()
        var push = Vector2(int(pixel_remainder.x), int(pixel_remainder.y))
        pixel_remainder -= push
        Input.warp_mouse_position(mouse_pos + push)

It looks like this:

enter image description here

by (140 points)

This is exactly what im looking for, but after copying the code it's just warps the mouse far away from the circle instead of pushing it. What couls be the problem?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.