Retain last joystick direction

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

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:
	update_movement()


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 ?

According to the documentation (Controllers, gamepads, and joysticks — Godot Engine (stable) documentation in English) you can use Input.get_vector() to provide the x and y axis.

Gluon | 2021-12-20 21:41

Oh, that’s handy, but that doesn’t resolve the multiple call problem…

Midonk | 2021-12-20 23:00

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.

spaceyjase | 2021-12-21 11:52