Character with multiple sprites doesn’t flip correctly when scaling the root node

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật