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.
scale.x = -1on the body: read back as scale (-1, 1), rotation 0, then scale (1, -1), rotation -180 after move_and_slide(). Holding left from a fresh start for 1 to 6 frames drew L, LR, LRL, LRLR, LRLRL, LRLRLR. After letting go it stayed on the last one for 5 frames: left after an odd number of frames, right after an even number.flip_honly: left on all 12 frames. The slash point stayed at +24 px and the hitbox at +20 px, behind the character. The enemy on the left was not hit, the enemy on the right was.- The script: left on all 12 frames and on the 5 frames after letting go, for every hold from 1 to 6 frames. The body kept scale (1, 1) and rotation 0. Slash point -24 px, hitbox -20 px: the enemy on the left was hit, the one on the right was not. Turned right: +24 px and +20 px, and the enemy on the right was hit.
- A slash effect added to the world at the slash point, character facing left: both ways it landed 24 px to the left, but set by position only it kept scale.x 1 and was drawn facing right. With spawn() it got scale.x -1 and faced left.
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
- Sprite art drawn facing left: turn
flip_hon once in the editor, so that a Visual scale of 1 means facing right. - Effects that should follow the character, like a sword trail or an aura, go under Visual. Effects that stay where they were made, like a slash or dust, go to the world with spawn().
- A body collision shape that is not centred: move it under Visual too only if it must flip, as the body's shape is what move_and_slide() collides with.
- Placing the slash at the point of impact instead: spawn a hit effect at the impact point. Hitting each enemy once per swing: sword hitbox hits each enemy once. Removing the slash when its animation ends: animation_finished never fires.
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
- A line or rectangle attack that hits what the player sees
- A scare that plays once, and only for the player
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A parry window that is the same length on every screen
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- A saw blade that slides along its path at a steady speed
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over