Godot Version
4.3
Question
I’m trying to make a game for a game jam and i’m figuring out the movement system. I also need to make the character do an animation when going in diagonal but the animation just freezes here’s a code snippet:
if Input.is_action_pressed("right"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left"):
$AnimatedSprite2D.play("walk_left")
if Input.is_action_just_released("left"):
$AnimatedSprite2D.play("idle_left")
if Input.is_action_just_released("right"):
$AnimatedSprite2D.play("idle_right")
if Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_down")
if Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_up")
if Input.is_action_just_released("down"):
$AnimatedSprite2D.play("idle_down")
if Input.is_action_just_released("up"):
$AnimatedSprite2D.play("idle_up")
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left")
if Input.is_action_pressed("right") and Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") and Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_left")
It’s a mess but the problem is in the last ligns, hope you can help.`
Have you tried singling out the code and seeing if it’s not working because it’s trying to play 2 animations at once?
1 Like
I think that’s what it’s doing, what can i do then ?
The slightly altered code :
func _process(_delta):
#idle
if Input.is_action_just_released("right"):
$AnimatedSprite2D.play("idle_right")
if Input.is_action_just_released("down"):
$AnimatedSprite2D.play("idle_down")
if Input.is_action_just_released("up"):
$AnimatedSprite2D.play("idle_up")
if Input.is_action_just_released("left"):
$AnimatedSprite2D.play("idle_left")
#up and down
if Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_up")
if Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_down")
#diagonals
if Input.is_action_pressed("right") && Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") && Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left")
if Input.is_action_pressed("right") && Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_right")
if Input.is_action_pressed("left") && Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_left")
maybe , because you use if
statement only?
try use if
and elif
or you can just code like this
var move_dir : Vector2 = Input.get_vector( “A_Key”, “D_Key” ,“W_Key”, “S_Key”)
then use move_dir to set animation
move_dir will give you 8 direction
Your mistake is using If statements only.
It’ll will evaluate ALL that match. So if you press up and left, it won’t play diagonal, it’ll fight with all for the animation player.
Handle Diagonals first, then under them handle single directions.
func handle_movement_animation():
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right_up")
elif Input.is_action_pressed("left") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left_up")
# Keep going with diagonals.
# Then use single directions checks for Up, right..etc
Then keep going…
else:
$AnimatedSprite2D.animation.begins_with("walk_right"):
$AnimatedSprite2D.play("idle_right")
# handle the idle logic here yeah?
1 Like
That’s what I was thinking but I was not in my computer so I could not type that all out
Thanks, it worked
I’m adding attacks next so i should be able to just add it behind the single move checks.
Here’s the working code:
func _process(_delta):
#diagonal checks
if Input.is_action_pressed("right") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_right")
elif Input.is_action_pressed("left") and Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_left")
elif Input.is_action_pressed("right") and Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_right")
elif Input.is_action_pressed("left") and Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_left")
#single move checks
elif Input.is_action_pressed("down"):
$AnimatedSprite2D.play("walk_down")
elif Input.is_action_pressed("up"):
$AnimatedSprite2D.play("walk_up")
elif Input.is_action_pressed("right"):
$AnimatedSprite2D.play("walk_right")
elif Input.is_action_pressed("left"):
$AnimatedSprite2D.play("walk_left")
#idle checks
elif $AnimatedSprite2D.animation.begins_with("walk_right"):
$AnimatedSprite2D.play("idle_right")
elif $AnimatedSprite2D.animation.begins_with("walk_down"):
$AnimatedSprite2D.play("idle_down")
elif $AnimatedSprite2D.animation.begins_with("walk_up"):
$AnimatedSprite2D.play("idle_up")
elif $AnimatedSprite2D.animation.begins_with("walk_left"):
$AnimatedSprite2D.play("idle_left")
Start looking into StateMachine
design, plenty of tutorials online.
It’s better to use that philosophy to handle player during “Fight”, “Walking”, and other states instead of trying to keep track of elif statements and their priorities in code.