How would I flip the walk animation in this case?

Godot Version

4.3

Question

I have this code for left and right movement but no matter what I can’t get the Animated sprite to flip when the player is moving the other direction.

var direction := Input.get_axis("left", "right")
if direction:
	velocity.x = move_toward(velocity.x, direction * speed, walk_speed * acceleration)
	$AnimatedSprite2D.play("walk")

I tried using

if velocity.x <0:
	$AnimatedSprite2D.flip_h

But that didn’t work an only messed up the movement. Thanks for any help in advance.

flip_h is a property of the sprite node. What you are currently doing is technically just extracting it’s value and then doing nothing with it.

if velocity.x <0:
	false # $AnimatedSprite2D.flip_h

You need to change flip_h into your desired value

if velocity.x < 0:
	$AnimatedSprite2D.flip_h = true
elif velocity.x > 0: # So sprite keeps most recent direction if character is idling
	$AnimatedSprite2D.flip_h = false
1 Like

Lovely, it works. One issue though, now my character slides towards the last direction I pressed, as in if i press right the player keeps sliding to the right even after I stop pressing the right button.

var direction := Input.get_axis("left", "right")
if direction:
	velocity.x = move_toward(velocity.x, direction * speed, walk_speed * acceleration)
	$AnimatedSprite2D.play("walk")
if velocity.x <0:
	true 
if velocity.x < 0:
	$AnimatedSprite2D.flip_h = false
elif velocity.x > 0:
	$AnimatedSprite2D.flip_h = true

Also I flipped the values because the sprite is originaly the other way around.

There is no need to if direction on velocity, this will only apply speed if the player holds a button down, but you also want to apply speed towards 0 when the player isn’t moving

var direction := Input.get_axis("left", "right")
velocity.x = move_toward(velocity.x, direction * speed, walk_speed * acceleration)

if direction:
	$AnimatedSprite2D.play("walk")
	$AnimatedSprite2D.flip_h = direction > 0 # maybe get a lil fancy here too
else:
	$AnimatedSprite2D.stop()
1 Like

It works! Thank you both very much I have been doing this for a whole 5 minutes so I really appreciate the help.