In GoDot, me and my friend are trying to flip_h the player sprite while also flipping an attack animation. When we try to scale.x = -1 and scale.x = 1 based on direction, the sprite doesn’t flip when running “right.” It flips constantly by moving left, though. We have been pulling our hair out trying to fix it.
Things tried:
func _input(event):
if event.is_action_pressed(“right”):
scale.x = sign(direction.x)
elif event.is_action_pressed(“left”):
scale.x = sign(direction.x)
if velocity.x > 0:
$“.”.scale.x = sign(direction.x)
else:
$“.”.scale.x = -sign(direction.x)
Tried * -1 for position of antimation with a normal Sprite flip_h
@onready var spr: Sprite2D = $"." # Assuming it's a Sprite2D
func _input(event):
if event.is_action_pressed("right"):
spr.set_flip_h(false)
if event. is_action_pressed("left"):
spr.set_flip_h(true)
To go further. Looking at your code, the sprite flip is skewed to always use right(unflipped) when both directions are pressed due to how your if/elif is structured. Because of the elif event.is_action_pressed("left"), the left check is never reached when right is pressed. So if you press left and right, it’s will not flip. This can be improved by cancelling out when both are pressed like so:
# Disregard Y code if it's not a top down game
var facing := Vector2(1, -1) # Initialized as right up
var direction: Vector2
func _input(event):
# Returns 0 when both left + right is pressed
direction.x = Input.get_axis("left", "right")
direction.y = Input.get_axis("up", "down")
if direction.x: # Direction is not 0
if direction.x < 0: facing.x = -1
if direction.x > 0: facing.x = 1
if direction.y < 0: facing.y = -1
if direction.y > 0: facing.y = 1
spr.set_flip_h(true if facing == -1 else false)
By setting a separate facing variable to a value of the last allowed directional value. You have the direction vector that reflects the player input and the facing bool to indicate the direction the player is facing when in idle/standing still. You can also do further checks to only set the facing/direction if the player is moving by adding velocity checks as well if you don’t want to play a running animation if the player isn’t actually moving.