0 votes

For now, I use _unhandled_input to retreive the Vector2 representation of a joystick input with this method

func _unhandled_input(event):
if event is InputEventJoypadMotion or InputEventKey:

func update_movement():
var move_direction : Vector2
move_direction.x = Input.get_axis("MoveLeft", "MoveRight")
move_direction.y = Input.get_axis("MoveUp", "MoveDown")

So I have 4 actions mapped and I virtualy create 2 axis in this update_movement method BUT the problem is to retain my previous move_direction when I release the joystick.

As the _unhandled_input is called for each input, the "x" axis will call, then the "y" which causes a double call that mess up with the joystick vector. So when one of them (x or y) is zero, the other will be set as the last direction of the joystick, forcing the orientation in one of the four cadinal points...

So my question is: is there a way to retreive joystick (x, y) axis directly in a Vector2 or is there another way to retain the last direction of the joystick when it got released ?

Godot version 3.4
in Engine by (167 points)

According to the documentation (https://docs.godotengine.org/en/stable/tutorials/inputs/controllers_gamepads_joysticks.html) you can use Input.get_vector() to provide the x and y axis.

Oh, that's handy, but that doesn't resolve the multiple call problem...

Move move_direction out of the function; i.e. it's a class member. You update the vector with each event as you do already.

At some point you may have to consider resetting it to zero (perhaps at the end of frame) but that will depend on your game.

Please log in or register to answer this question.

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.