Why is my character not flipping?

Godot version 4.2.1

My character isn’t flipping through “flip h” is on. Please help. code below

<extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
@onready var anim = get_node(“AnimationPlayer”)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis(“ui_left”, “ui_right”)
if direction == -1:
get_node(“CollisionShape2D/AnimatedSprite2D”).flip_h = true
elif direction == 1:
get_node(“CollisionShape2D/AnimatedSprite2D”).flip_h = false

if direction:
	velocity.x = direction * SPEED * 3
	if velocity.y == 0:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	if velocity.y == 0:
		if velocity.y > 0:

Did you try to print your direction ? In your current code, the character only flips when the direction is exactly 1 or -1. But get_axis returns a float. so maybe its 0.1 or -0.1 (I dont know if you use a controller or something)

You could try :

if (direction > 0):
    get_node(“CollisionShape2D/AnimatedSprite2D”).flip_h = false
    print(direction) # to see if you hit your condition
elif(direction < 0 ):
    get_node(“CollisionShape2D/AnimatedSprite2D”).flip_h = true
    print(direction) # same thing here

I use just a computer keyboard as for controls. Sadly your code didn’t work. :frowning:

What is getting printed?

And please fix you code formatting.
The error might even be in the way you formatted the code.
The code tag button looks like this </>

Haven’t used get_axis for a while, but why don’t you use Input.is_action_just_pressed(“ui_left”)?

As mentioned. Look what the print function says