How do i make a variables result stay the same without the condition being on everysingle time

Godot Version

4.2

Question

So I’m trying to make a moving scriptand I’m trying to flip the characters sprite but the thing keeps reverting back to its first value

Context: I have a condition if velocity.x <= 0.0 it flips and vice versa but it keeps reverting back to its original value because the thing only turns into that value while velocity.x <= 0.0

Is there a way i can fix this and flip the charcter while keeping the value the same (also sorry for my bad writing im In a rush)

Without seeing your code we could only speculate what the problem might be.

I’d recommend reading the Posting guidelines in #Help channel topic, for some tips on helping us to help you.

Sorry this is my code

If velocity.x <= 0.0
    $AnimatedSprite2D.flip_h = true 
    Lookdir = -1.0
If velocity.x >= 0.0
   AnimatedSprite2D.flip_h  = false
   Lookdir = 1.0

Sorry if code stuff looks bad idk how to add tabs on mobile

I don’t fully understand your problem. But if your problem is: “if my character moves to the left and then comes to a full stop, the character’s sprite flips to face the wrong way”, then that’s explained by your code.

If velocity.x equals 0.0 exactly, then both if statements are true. The sprite’s flip_h property will first be set to true, and then on the same frame it will be set to false.

Consider using the last input direction to figure out which way the sprite should face instead of the character’s current velocity.

If your velocity is 0, then both if statements are true so the last if statement is going to override the previous one. You can instead do this:

if velocity.x < 0:
    $AnimatedSprite2D.flip_h = true 
    Lookdir = -1.0
elif velocity.x > 0:
    $AnimatedSprite2D.flip_h = false
    Lookdir = 1.0

This way, the conditions don’t conflict because instead of using >=, we used > only. So the character should stay at the last flip_h state when it comes to a stop. Also used elif, because if velocity is lower than zero, you don’t need to check it again for the second condition.

One more thing, 0 tends to be unreliable for a check condition because of floating point errors, so if you still encounter flip issues, you can change the conditions to this

if velocity.x < -0.01:
   ...
elif velocity.x > 0.01:
   ...

thanks my code now works

FYI, you can also use is_equal_approx or is_zero_approx