How to prevent top down sprite snapping during diagonal movement upon keyboard release

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

I have a sprite that faces 4 directions depending on input. This is all working well except for my keyboard input…

If I am pressing down-right at the same time on the keyboard and release both keys there is a moment where I am pressing only the right key. This creates a snapping of my sprite upon key release where now im suddenly facing right instead of down.

basically i would like to release both the x and y buttons at the same time but i dont have a good way of detecting this. its TOO sensitive.

    	pressed_vector.x = int(directions.right) - int(directions.left)
		pressed_vector.y = int(directions.down) - int(directions.up)

		dpad_vector = pressed_vector.normalized()
		owner.direction_changed(dpad_vector)

its the standard input code you see everywhere.

I have the player mapped to a virtual joystick, dpad etc and i dont have this issue because on release you’re going to be releasing your x and y keys at the same time. not sure how to handle this with the keyboard.

:bust_in_silhouette: Reply From: Sween123

I suggest you to use an array to control the directions and the function process() to update the movement. That’s the way how I control motions. This way the character can go smoothly without errors.
Let’s say this array is DIR = , The idea is whenver you press a direction key, push_front() it into DIR, whenever you release a direction key, erase it from the DIR (For Example, DIR.erase(“Up”)). In the function process(), which is the updating function, you simply move the character to the direction DIR[0] if there are any directions in the array, or not moving if DIR.size() == 0.

While this is one way to implement 4-way-controls, it is not an answer to the question!

njamster | 2020-02-07 17:02

Oh right. Sorry about that. That’s for straight line movement…
Thanks for reminding.

REEDITED:
I suggest you to use an array to control the directions and the function process() to update the movement. That’s the way how I control motions. This way the character can go smoothly without errors.
Let’s say this array is DIR = , The idea is whenver you press a direction key, push_front() it into DIR, whenever you release a direction key, erase it from the DIR (For Example, DIR.erase(“Up”)). In the function process(), which is the updating function, update the character’s facing direction to DIR[DIR.size() - 1].

Sween123 | 2020-02-07 17:43

:bust_in_silhouette: Reply From: njamster

Assuming the code you posted is part of _process() anddirection_changed() simply sets the sprites rotation based on the value of dpad_vector, you should first ensure, that it isn’t called when dpad_vector is zero, as this will reset the rotation when no keys are pressed. This might solve your problem already.

If not, you’re best bet will be to delay the call to direction_changed by a few frames. Make sure to store the dpad_vector of the previous frame globally and each frame compare it to the current one. If they are the same, reduce a counter variable by 1, if not reset it to it’s default. Once the counter reaches 0 (and only then!) call direction_changed.