Help with transitioning to Idle Animations

Godot Version

Godot_v4.5.1

Question

Hi, I'm a beginner developer trying to make a short story-based RPG game in Godot. I hope to be able to learn about game development from everyone here, and thank you for your help in advance! My main struggle at the moment is that I don't know how to make the player face the direction they were last at when they go to the Idle Animation.I have 6 different animations in my Animated Sprite 2D:

  1. IdleB (Idle and facing the camera)
  2. IdleS (Idle and facing left)
  3. IdleF (Idle and facing away from the camera)
  4. WalkB (Walking to the camera)
  5. WalkS (Walking to the left)
  6. WalkF (Walking away from the camera)

This is my code I have at the moment, it only has walking animations as I am not sure how to implement idle animations smoothly.

I apologize if this question seems a bit dumb, but I really am stuck here. Once again, I’d like to thank anyone in advance for helping, and I hope to learn much from trying to make this game (and asking more questions when I am stuck)!

you could detect if neither direction is pressed

if directionF:
    # movement
if directionS:
    # movement
if directionF == 0.0 and directionS == 0.0:
    # idle animation

Make sure to paste code instead of screenshots

You also can use var direction = Input.get_vector("left", "right", "up", "down")

That function gives you a normalized Vector2 with your input values.

You can compare it with an Vector2.ZERO to know if is not pressed any direction:

if direction == Vector2.ZERO:
    
    print("Idle animation")
else:
    print("Walk animations")

To know if is Up, down, right or left use direction.x / dirección.y

And for getting the correct idle animation from the three you have, just use a separate variable to store the last directional input. Always set this variable depending on the directional input, unless it’s a zero vector.

Then use this variable to play the correct idle animation.