Mouse warping to the corner of the screen

Godot Version

4.3

Question

I want for mouse cursor to randomly move to a random direction. There’s the problem. This is a part of a character script:

func _ready():
	screen_size = get_viewport_rect().size
	
func _on_character_body_2d_move_mouse():
        rndMousePos = get_global_mouse_position()
	print(rndMousePos)
	print(self.global_position)
	rndMousePos = get_global_mouse_position()
	var rndVelocity = rnd.randf_range(-mouseOffset, mouseOffset)
	var coin = rnd.randf_range(0, 1)
	var newMouse = rndMousePos
	print(newMouse)
	if coin <= 0.5:
		newMouse.x += rndVelocity
		get_viewport().warp_mouse(clamp(get_global_mouse_position() + (newMouse - get_global_mouse_position()), Vector2.ZERO, screen_size))
	else:
		newMouse.y += rndVelocity
		get_viewport().warp_mouse(clamp(get_global_mouse_position() + (newMouse - get_global_mouse_position()), Vector2.ZERO, screen_size))

When this script is opened on a player scene, mouse moves as it need, but at the global scene it infinitely flings cursor at the left corner of the windows
How do i fix it?

It is the clamps they have a dumb behavior with vectors. If it activates on one axis it modifies both x and y.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.