0 votes

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

in Engine by (196 points)

1 Answer

+3 votes

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")
by (3,505 points)

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

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")
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 Frequently asked questions and 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 [email protected] with your username.