detect mouse direction

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

i use Input(event) and InputEventMouseMotion, but how do i know if the mouse moves left or right

func _input(event):
if event is InputEventMouseMotion:
	if event.relative:
		a += 1 
		a -= 1
		print(a)

I want that when I move the mouse to the right it adds the variable and when I turn to the left it subtracts, detect the negative and positive side. for now the variables are added and subtracted at the same time since I can’t get the mouse side for each one

:bust_in_silhouette: Reply From: p7f

Hi! the relative property of InputEventMouseMotion already gives you the information about the direction you are moving. If the x component of relative is less than 0, you are moving left, if it is greater than 0, you are moving right. Same with y component for up and down. For example:

func _input(event):
    if event is InputEventMouseMotion:
    	if event.relative.x > 0:
    		print("Moving right")
    	if event.relative.x < 0:
	    	print("Moving left")

But… If I move the mouse from say -78 to -56, I’ve moved it to the right, yet I’m still <0…

Macryc | 2021-01-03 15:14

godot 3.4.4

func _input(event):
	if event is InputEventMouseMotion:
		if event.get_relative().x > 0:
			print("Moving right")
		if event.get_relative().x < 0:
			print("Moving left")

Zelta | 2022-04-07 11:04