AnimatedSprite2D not Flipping

Godot Version

4.1.1

Question

`I’m trying to flip the character Sprite with Flip h but it isn’t flipping in the DEBUG, I’m following Brackeys tutorial and using the basic movement template script. This is my code:

extends CharacterBody2D

const SPEED = 75.0
const JUMP_VELOCITY = -400.0

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity.y += get_gravity().y * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction: 1, 0, -1
	var direction := Input.get_axis("move_left", "move_right")

	# Flip the Sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true

	# Apply movement
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

Have you tried printing the value of direction to the console with print(direction) if it is greater than 0 or less than 0? If not then try it.

I have tried the print true/false in the output test but it didn’t work: A & D inputs don’t work period.

Hi,

If the inputs are not working, the issue would be on this line: Input.get_axis("move_left", "move_right"). However, the code looks fine to me, so the problem might be related to the input defined in the project input settings.
Are you sure the movement inputs have been setup correctly in the project? If so, could you share what you’ve done so that we can double check?

1 Like

You can also turn this:

	# Flip the Sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true

. . . into this:

	# Flip the Sprite
	animated_sprite.flip_h = direction < 0

(That won’t fix your current problem. @sixrobin 's solution is probably the one to solve your input issue. Project → Project Settings → Inputs.)

1 Like

@dragonforge-dev That’s not exactly the same code, as the sprite will be flipped if the direction is equal to zero. Flipping it only if there’s an actual direction will ensure the character keeps its last direction when stopping.

You are correct, it should do that. It does not. For whatever reason when you stop the sprite remains facing the correct direction. I used it in this game jam entry using an AnimatedSprite2D and it works fine. Eternal Echoes by Dragonforge Development

Really? :face_with_raised_eyebrow: That seems very strange to me. Not that I don’t trust you, but I just feel like this looks like an engine bug or something.
I would at least write something like this just for clarity purpose.

if direction != 0:
    animated_sprite.flip_h = direction < 0

But if the code works fine without the condition, well then, why not!

2 Likes

Seemed strange to me. I came across it in a class on GameDev.tv about making a multiplayer game in Godot. I disagreed with a LOT of the coding decisions he made, but this was one that worked so I adopted it.

1 Like