• Projects
  • setting mouse position wrong value

hello, iam trying to set the mouse position to an x,y

but this func doesnt seem to work: get_viewport().warp_mouse( btn_name.get_global_position() )

print("mouse1: " + str(   get_global_mouse_position()    ) );
print("mouse2: " + str(   get_viewport().get_mouse_position()     ) );

log:
mouse1: (501.799957, 247.999939)
mouse2: (976, 312)

Is there another way to set the mouse global position ?

Hi,

Maybe you could try Input.warp_mouse_position()?

That other way you show may also work, but the are relative to viewport, so maybe you need to calculate coordinates a bit differently? It's hard to tell not knowing scene setup.

@GlyphTheWolf said: Hi,

Maybe you could try Input.warp_mouse_position()?

That other way you show may also work, but the are relative to viewport, so maybe you need to calculate coordinates a bit differently? It's hard to tell not knowing scene setup.

it doesnt work, Input.warp_mouse_position() goes to the value printed by get_viewport().get_mouse_position() to do that i would have to use get_viewport().get_node().position... and its impossible

best solution i could find is node_name.get_global_transform_with_canvas() this returns a "transform2D cant be converted to vector2"

node_name:((-0, -3.333333), (3.333333, -0), (936.666809, 308.66687)) mouse:(936.666687, 308.666687)

Ok, so you need to use probably:

Input.warp_mouse_position(node_name.get_global_transform_with_canvas().get_origin())

That should warp mouse to the left upper corner of control unless origin is set differently.

Check this project. You can press WASD to move the mouse. To get it to work with both a real mouse and a fake mouse, you have to make a fake mouse cursor. The code should explain everything.

Check this project. You can press WASD to move the mouse. To get it to work with both a real mouse and a fake mouse, you have to make a fake mouse cursor. The code should explain everything.

Check this project. You can press WASD to move the mouse. To get it to work with both a real mouse and a fake mouse, you have to make a fake mouse cursor. The code should explain everything.

extends Node2D

onready var cursor = get_node("Cursor")
var mouse_pos = Vector2()
var mouse_speed = 3.0

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)

func _process(delta):
	var mouse_rel = Vector2.ZERO
	if Input.is_action_pressed("mouse_up"):
		mouse_rel += Vector2.UP * mouse_speed
	elif Input.is_action_pressed("mouse_down"):
		mouse_rel += Vector2.DOWN * mouse_speed
	elif Input.is_action_pressed("mouse_left"):
		mouse_rel += Vector2.LEFT * mouse_speed
	elif Input.is_action_pressed("mouse_right"):
		mouse_rel += Vector2.RIGHT * mouse_speed
	if mouse_rel != Vector2.ZERO:
		Input.warp_mouse_position(mouse_pos + mouse_rel)
	cursor.position = get_global_mouse_position()

func _input(event):
	if event is InputEventMouseMotion:
		mouse_pos = event.position

@cybereality said: Check this project. You can press WASD to move the mouse. To get it to work with both a real mouse and a fake mouse, you have to make a fake mouse cursor. The code should explain everything.

extends Node2D

onready var cursor = get_node("Cursor")
var mouse_pos = Vector2()
var mouse_speed = 3.0

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)

func _process(delta):
	var mouse_rel = Vector2.ZERO
	if Input.is_action_pressed("mouse_up"):
		mouse_rel += Vector2.UP * mouse_speed
	elif Input.is_action_pressed("mouse_down"):
		mouse_rel += Vector2.DOWN * mouse_speed
	elif Input.is_action_pressed("mouse_left"):
		mouse_rel += Vector2.LEFT * mouse_speed
	elif Input.is_action_pressed("mouse_right"):
		mouse_rel += Vector2.RIGHT * mouse_speed
	if mouse_rel != Vector2.ZERO:
		Input.warp_mouse_position(mouse_pos + mouse_rel)
	cursor.position = get_global_mouse_position()

func _input(event):
	if event is InputEventMouseMotion:
		mouse_pos = event.position

it still doesnt work... has soon has i press w,s,d,a the cursor the cursor gets teleported to the upper left screen... if iam in window mode the cursor goes all the way down to the bottom right out of the window view...

i just wanted to do a basic thing... move the player with the mouse if the button is pressed...

it works fine... but frist i must put the mouse cursor in the position of the player offset, or when i click the player, he gets slightly teleported a few pixels to were mouse is, before he starts moving

if ( Input.is_action_just_pressed('mouse_button'): mouse = player.global_position; elif ( Input.is_action_pressed('mouse_button'): player.position = mouse.global_position;

i think the problem has something to do with this settings the same problem happens in your project mouse cursor