How to flip a KinematicBody2D left and right

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Newby

I have an area2d child for my kinematicbody 2d as my attackbox and i want it to flip when the player flips.

:bust_in_silhouette: Reply From: kidscancode

If you use scale.x *= -1 on the KinematicBody2D, its children will be flipped as well.

I tried that but for some reason my player keeps flipping back and forth if i try to move left.

Newby | 2019-06-16 08:03

Well that’s the answer. Since you didn’t show what you did, I can’t tell you what you did wrong.

kidscancode | 2019-06-16 08:05

No it’s not the answer. Kinematic bodies cannot be flipped that easily.

https://github.com/godotengine/godot/issues/12335

Arthur Paulino | 2020-01-04 18:21

I tried that but for some reason my player keeps flipping back and forth if i try to move left.

that’s because you are flipping it once every frame.

Snail0259 | 2021-08-07 07:25

:bust_in_silhouette: Reply From: Dlean Jeans

Pseudo code:

if is_moving_left:
    scale.x = -1
elif is_moving_right:
    scale.x = 1
:bust_in_silhouette: Reply From: k3nzngtn
if _velocity.y < 0:
    scale.x = scale.y * -1
elif _velocity.y > 0:
    scale.x = scale.y * 1

This also keeps the body from flipping.

Source: Issue with flipping 2D characters (non uniform scaling) · Issue #12335 · godotengine/godot · GitHub

:bust_in_silhouette: Reply From: 439Games

I was struggler to flip my kinematic body, but i found to manage to be able to doing it the rigth way with all the answers i read.

enter image description here

onready var animated_sprite = $AnimatedSprite
onready var melee_weapon = $Hitbox

if velocity.x > 0:
	melee_weapon.scale.x = 1
	animated_sprite.flip_h = false
elif velocity.x < 0:
	melee_weapon.scale.x = -1
	animated_sprite.flip_h = true

and that work for me.

If i understand, move_and_slide() reset each frame scale.x to 1. Im not sure but i seem to read somewhere this reason for the flickering.

My first answer, yeah :smiley: