0 votes

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.

asked Jan 20 in Engine by hamoom (35 points)
edited Jan 20 by hamoom

2 Answers

0 votes

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.

answered Feb 7 by Sween123 (758 points)

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

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].

0 votes

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.

answered Feb 7 by njamster (9,824 points)
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 How to use this Q&A? before posting your first questions.