0
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

I can see two ways to avoid this problem:

  • Base the player animation decision on the sprite's effective velocity (speed and direction), rather than pressed input keys. This can be a good idea if using movement with relatively low friction and acceleration.
  • Add a Timer node set to Oneshot that will act as a debounce timer. Its wait time should be very low (0.05 seconds or less) and should be reset every time a movement key is pressed or released. Only apply movement animation changes when the timer node times out by connecting its timeout signal to a function in your player script.
\$\endgroup\$
1
  • \$\begingroup\$ debounce timer :) why didnt i think of that \$\endgroup\$ – hamobi Feb 13 '20 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.