`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()
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?
@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? 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!
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.