8 way movement GoDot Question

Hello, I was hoping to get some guidance/assistance with this problem I am having in Godot.
I started a project where I want to create 8 way movement for my character.
The scene I’ve created consists of: CharacterBody2D, Camera2D, Area2D (with CollisionShape), CollisionShape2D (2 total), and an AnimatedSprite2D.

The issue:
When I stop moving my character doesn’t idle, he does transition to run, and can use the attack animation, but he won’t idle if I attach more than two animations I can do two, but then the third seems to be left out.

I tried several ways of fixing the movement the way I wanted and the solution I’ve come up with so far is to use get_vector with the four arguments being the four inputs (move_forward/backward, left/right)

Here is what I wrote currently:

I tried initially putting everything in _physics_process, but it becomes an if soup (and already is).
I also tried saving Input.get_vector to a variable named direction, and then making velocity = direction * speed, but this didn’t work for some reason?

I believe the issue is because idle is taking place in a delta process it is being constantly updated, so it cancels my attack move. Should I create an attack entirely separately even though its animated through my player character?

if velocity.x or velocity.y != 0:

Ah, that’s an easy mistake to make when you haven’t encountered it yet. This doesn’t work quite the same way in code as it does in English. What you’ve written here is shorthand for the following (brackets added for clarity):

if (velocity.x != null) or (velocity.y != 0):

(Might not specifically be null, but definitely something similar and you get the idea). Anyway, because the left side of the OR conditional is basically a tautology (always true), your else statement will never be reached. You will need to change the left side to be explicit, something like this:

if velocity.x != 0 or velocity.y != 0:

It’s actually the reason why I personally consider the double pipe || to be more human-readable than or. Since it’s less relatable to English, it’s harder to make that misinterpretation with || when you’re tired and not thinking clearly, but both are perfectly valid.

You might also want to change change line 16 from if to elif, although that’s unrelated =)

Finally, I’m assuming you will want your attack animation to take priority:

You’ve got the right idea - the AnimationPlayer is being set to the idle animation before the attack animation can finish. To give the attack animation priority over the other two, I would put the 4 lines from 18-21 in another if-statement that checks if the attack animation is not playing. Since I’ve only just introduced this idea, give that a try on your own first and see if you can get that working.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms