flipping my character

Godot Version

4

Question

I am making a 2d platformer, but I can’t seem to get flipping characters down. I want the player to visually flip, but I also want the weapon, and hitboxes to flip too, I tried just scaling the player, but that didn’t work because the player would only flip when moving left. if anyone can help me, that would be much appreciated

Hi Andrew,

I am trying to understand what you mean by flip, but the term is a bit vague. What do you want to flip, the facing direction of your character? If you could provide more info that would make it easier to identify how to solve your problem.

Kind regards :four_leaf_clover:

yes, the direction I am facing. sorry for not being cleat enough. my bad

You should not change the scale of the player itself, since that will cause issues with the physics. Instead, you can use the sprite’s flip_h property and move and rotate the character’s weapon. For the hitboxes, you could have two hitboxes, and you just disable the one you’re not using for the current direction.

2 Likes

Depends on what nodes you are using. But typically as @paintsimmon pointed out you can use the flip_h property. As for the hitbox, etc, multiply their position them by -1 on the x-axis. The key is to do nothing if there’s no input. Here’s an example I pulled from enemy code:

extends Character2D

var direction: float = 0.0

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var ranged_attack_area: Area2D = %"Ranged Attack Area"
@onready var detection_area: Area2D = %"Detection Area"
@onready var melee_attack_hit_box: Area2D = %MeleeAttackHitBox


func _physics_process(delta: float) -> void:

# .
# . Get input and update direction/velocity here
# .
	
	move_and_slide()
	
	if direction != 0:
		_flip(direction)
	direction = 0.0


func _flip(direction_float: float) -> void:
	animated_sprite_2d.flip_h = direction_float < 0
	_flip_area(melee_attack_hit_box, direction_float)
	_flip_area(ranged_attack_area, direction_float)
	_flip_area(detection_area, direction_float)


func _flip_area(area: Area2D, direction_float: float) -> void:
	area.position.x = abs(area.position.x)
	if direction_float < 0:
		area.position.x *= -1
3 Likes

I don’t think “scale” is the right word to describe what you do to the Area2D since you’re not changing the scale property, you are changing its position.

@paintsimmon You are correct. Post updated. I wrote that then pulled the code from the project. It was like 4am when I posted that.

1 Like

thank you guys. this really helped

1 Like