Cahracer animation flip_h always true when there is no input

Godot Version

<4.2.2 stable

Question

i tried everything i know and it still dosent work
code:
extends CharacterBody2D

var d = 0
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var animatedS = $AnimatedSprite2D

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

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func flip(d):
var flip_test = true
if d == -1 :
flip_test = true
else:
flip_test = false
return flip_test

func animation(d):
var t = flip(d)
animatedS.set_flip_h(t)

if not is_on_floor():
	animatedS.play("jump")
	var f = animatedS.get_frame()
	if f == 7 :
		animatedS.pause()
	
elif d != 0:
	animatedS.play("walk")
elif d == 0 :
	animatedS.play("idle")

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")
d = direction
if direction:
	velocity.x = direction * SPEED
	
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
animation(d)


move_and_slide()

What are you trying to do? Explain your question pls. Also, put your code in the </> preformatted text to make it better to read.
‘’’

type or paste code here

‘’‘’

Are you sure it’s always true? Could you explain what you expect to happen vs what really happened?

when i move the character to the left the sprites flips horizontaly like it should
when i relese the input the character turns to the left

extends CharacterBody2D

var d = 0
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var animatedS = $AnimatedSprite2D

#Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func flip(d):
	var flip_test = true
	if d == -1 :
		flip_test = true
	else:
		flip_test = false
	return flip_test

func animation(d):
	var t = flip(d)
	animatedS.set_flip_h(t)

	if not is_on_floor():
		animatedS.play("jump")
	var f = animatedS.get_frame()
	if f == 7 :
		animatedS.pause()
	
	elif d != 0:
		animatedS.play("walk")
	elif d == 0 :
		animatedS.play("idle")
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")
	d = direction
	if direction:
		velocity.x = direction * SPEED
		
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	animation(d)


	move_and_slide()

i didnt get how to use the preformated thing
the big text is a comment

Also, there is a place which says type or paste code here. Just delete that and put your code between the three dots that appear above and below it. Make sure that the first 3 are on a line above the code and the second three are on the line below. Those lines must not have any letters on them. You seem to have only used one for in place of the three that appear. There is a button with the </> symbol on the top bar, or if your on a mobile, the setting looking thing on top of the text box.

1 Like

I see an you do not want it to flip back when released, you want it to stay facing the last direction?

Instead of if/else (one branch will always trigger) use an if/elif where 0 has no affect.

func animation(d: float) -> bool:
    if d > 0:
        animatedS.flip_h = false
    elif d < 0:
        animatedS.flip_h = true

You must paste code between three backticks like so, pressing the </> button on a new line or ctrl+e will create the backticks for you.

```
type or paste code here
```

thanks it woks now!!!