← Pixel Dreamer Studio

Turn a 2D character left without the flicker

Flip one child node that holds the sprite, the hitbox and the spawn points, and leave the body's scale alone.

Godot cannot keep a scale of -1 on the x axis of a 2D transform: it reads it back as a 180 degree turn with the y axis flipped. In our Godot 4.7 test a CharacterBody2D read back scale (-1, 1) right after scale.x = -1, and scale (1, -1) with rotation -180 after move_and_slide(). Set it again on the next frame and the body turns back, so holding left drew it left, right, left, right, and which way it faced after you let go depended on how many frames you held. flip_h on the sprite stopped the flicker, but only the sprite turned: the slash point and the hitbox stayed on the right and the attack hit the enemy behind. The script below flips one child node instead: left on every frame, slash and hitbox in front.

The code

Save it as facing.gd and load it with const Facing := preload("res://facing.gd"). Put the Sprite2D or AnimatedSprite2D, the hitbox Area2D and any Marker2D spawn points under one Node2D child named Visual. The body's own CollisionShape2D stays directly under the body. In _physics_process() call Facing.turn($Visual, velocity.x) before move_and_slide(). For a slash that should stay where it was made, add it with Facing.spawn(SLASH.instantiate(), $Visual/SlashPoint, get_parent()).

extends RefCounted
## Turn a 2D character left or right without touching the body's own scale.
## Put the sprite, the hitbox and the effect spawn points under one child Node2D ("Visual")
## and flip that node, never the CharacterBody2D itself.
##   const Facing := preload("res://facing.gd")
##   func _physics_process(delta):
##       velocity.x = Input.get_axis("left", "right") * SPEED
##       Facing.turn($Visual, velocity.x)
##       move_and_slide()
##   func slash():
##       Facing.spawn(SLASH.instantiate(), $Visual/SlashPoint, get_parent())


static func turn(visual: Node2D, direction_x: float) -> void:
	if is_zero_approx(direction_x):
		return  # standing still: keep the way it faces now
	visual.scale.x = absf(visual.scale.x) * signf(direction_x)


static func spawn(effect: Node2D, point: Node2D, parent: Node) -> Node2D:
	## Add an effect to the world at a spawn point under the flipped node, facing the same way.
	parent.add_child(effect)
	effect.global_position = point.global_position
	effect.scale.x = absf(effect.scale.x) * signf(point.global_transform.x.x)
	return effect

What we measured

A CharacterBody2D with a sprite drawn facing right, a slash point 24 px in front and a hitbox 20 px in front, moved left at 100 px/s. We read which way the sprite was drawn after every physics frame, then put one enemy area on each side and asked the hitbox which one it touched. Two runs gave the same results.

Why it happens

A 2D transform stores two axes, not a scale and an angle. Flipping the x axis gives the same numbers as turning 180 degrees and flipping the y axis, so once the transform is written as a whole, the flip comes back as the second one. That is what we read after move_and_slide(). Your next scale.x = -1 then flips a body that is already turned around, and it faces right again. A child Node2D only moves with its parent, so its scale.x of -1 stays as you set it. flip_h has the opposite problem: it only changes how the texture is drawn, so nothing under the sprite moves to the other side.

What to change

Ready-made slashes: Pixel VFX Vol 1 Hits & Slashes has 12 pixel art combat effects in 3 colors: slashes, claws, thrusts, impacts, sparks, parry and shield, 64x64 and Godot 4 ready.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Pixel VFX Vol 1 Hits & Slashes has 12 pixel art combat effects in 3 colors: slashes, claws, thrusts, impacts, sparks, parry and shield, 64x64 and Godot 4 ready.

More how-tos: Hazards, traps and attacks