Context: I’m trying to code a top down movement system and I can’t get diagonal movement to work. Why does it only play the first frame? I checked the docs and it still won’t work
Couple of things:
- Post code using the brackets. Reading a video is hard.
- Dont use “ui_left”, etc. Those should be dedicated to UI.
That being said… the reason is because you are restarting the walk animation while the key is pressed.
When you check If statement, what’s inside runs if the conditions is true, and it skips what’s inside if condition is false. As they are right now, first 4 Ifs arent exclusive with eachother so if you hold Down + Right → If Left is skipped / If Right is executed [Animation set to “walk_right”] / If Up is skipped / If Down is executed [Animation set to “walk_down”]
When you call play() it always starts animation from the first frame unless you play same animation that is already playing. So if you hold down Down-Right at the start current animation is “walk_down”, then it changes to “walk_right” and back to “walk_down” again. That’s why animation is always stuck on first frame if you hold 2 buttons at once.
There is also quite a few issues other than just animation being stuck in this chunk of code. Generally you should keep logic and visual seperate - Simplest fix to animation would be to just have If-Elses on them like so:
if Input.is_action_pressed("left"):
# Stuff for left move
pass
elif Input.is_action_pressed("right"):
# Stuff for right move
pass
...
else:
# Stuff for idle (none of the movement commands pressed)
But in your current setup this would disable the ability to move diagonally at all.
Also read up on what normalize() does, because you might not realize it right now, but it’s not rly doing anything for you right now. I’d advise you to try to make a variable for direction, set it based on inputs and then normalize it’s value, fininishing off with:
velocity = direction * speed
I hope this helps - I don’t want to drop you finished code yet, because you’re clearly new to programming so I hope you can try to figure this out yourself
Hm… I think I’ll just disable diagonal movement. I like RPGMaker games so this would just be a nice homage. HOPEFULLY I figure this out later
I think making diagonal movement for simple top-down like this, is not something that you need to remove. It’s quite simple to do, once you know the general steps and if you can’t figure it out yourself, I can give you a working solution for you to use later. I just thought it would encourage you to try to figure out a solution yourself after I told you why animation is stuck.
Like I said - try to make a variable for direction, set it based on inputs, then normalize it and use it to set velocity at the end.
I’m quite new to Godot too, but this is simple enough task, that I feel comfortable to help if you if you rly feel like you can’t make progress. Good luck.
I’ll keep that in mind, thank you.
Yoo… just wanted to check-in if you’ve tried to figure this out