I’m working on a 2D game in Godot (version 4.3) and have a character composed of multiple sprites (e.g., body, head, equipment). These sprites are all children of a CharacterBody2D root node.
I want to flip the character so that it faces left or right depending on the movement direction. I’m trying to achieve this by changing the scale.x of the root node, expecting all child sprites to flip accordingly.
Here is my current code:
extends CharacterBody2D
@export_group("Instances")
@export var animationPlayer: AnimationPlayer
const SPEED = 150.0
var facing_direction = 1 # 1 means right, -1 means left
var initial_scale_x # Variable to store the original scale
func _ready():
animationPlayer.play("Idle")
initial_scale_x = scale.x # Store the original scale
func _physics_process(_delta):
var direction = Vector2.ZERO
# Collect input for movement direction
if Input.is_action_pressed("ui_right"):
direction.x += 1
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_down"):
direction.y += 1
if Input.is_action_pressed("ui_up"):
direction.y -= 1
# Movement and animations
if direction.length() > 0:
animationPlayer.play("Run")
velocity = direction.normalized() * SPEED
else:
animationPlayer.play("Idle")
velocity = Vector2.ZERO
# Flipping the character based on movement direction
if direction.x != 0 and facing_direction != sign(direction.x):
scale.x = initial_scale_x * sign(direction.x)
facing_direction = sign(direction.x)
move_and_slide()
Problem:
The character flipping doesn’t work as expected:
- Initial state: The character faces right.
- When I move left: The character correctly flips to face left.
- When I move right again: The character doesn’t flip back to face right and remains facing left.
- When I move left again: The character flips again, so it ends up facing right while moving left.
What I’ve tried:
- Changing the scale.x of the root CharacterBody2D node, assuming all child sprites would flip with it.
- Storing the original scale (initial_scale_x) and using it when flipping.
- Ensuring that facing_direction is updated correctly.
Question:
Why isn’t my character consistently flipping between left and right when I change the scale.x of the root node? Am I missing something? How can I correctly flip my character with multiple sprites without adjusting each sprite individually?
Additional Information:
- The character consists of multiple Sprite2D nodes directly under the CharacterBody2D node.
- There are no other parts of the code affecting the scale or orientation.
- I’m using move_and_slide() for movement based on velocity.
- The animations are working fine; the issue is only with flipping the sprites.
I’m grateful for any hints or solutions!
The problem is, that scale.x does not do what you think it does. See the documentation:
“Negative X scales in 2D are not decomposable from the transformation matrix. Due to the way scale is represented with transformation matrices in Godot, negative scales on the X axis will be changed to negative scales on the Y axis and a rotation of 180 degrees when decomposed.”
So by setting scale.x to -1, what really happens is that your scale.y is set to -1 and the rotation is set to 180.
So funny enough your code will simply work if you replace:
scale.x = initial_scale_x * sign(direction.x)
with
scale.x = -1