Character doesn't flip properly

Godot v4.2

Can someone tell me how to get this character setup to flip correctly?

Currently, each of the characters have a scene similar to this:

image

The movement component is the node responsible for moving the characters, it has the following script:

extends Node2D

@onready var character_speed = get_parent().speed
@onready var character = get_parent()

var velocity = Vector2.ZERO

func _ready():
	pass

func _process(delta):
	handle_movement()

func handle_movement():
	velocity = Vector2.ZERO
	
	if(Input.is_action_pressed("move_right")):
		velocity.x = 1
	if(Input.is_action_pressed("move_left")):
		velocity.x = -1
	if(Input.is_action_pressed("move_down")):
		velocity.y = 1
	if(Input.is_action_pressed("move_up")):
		velocity.y = -1
	
	if(velocity.length() > 0):
		velocity  = velocity.normalized() * character_speed
	
	if(velocity.x > 0):
		character.scale.x = 1
	if(velocity.x < 0):
		character.scale.x = -1


func get_velocity():
	return velocity

I try to flip the character scale here:

        if(velocity.x > 0):
		character.scale.x = 1
	if(velocity.x < 0):
		character.scale.x = -1

But when I try to do so, the character quickly flips between left and right. BUT only when moving to the left.

Instead of referencing the character body, reference the sprite 2d, and toggle its flip_x value.

2 Likes

I need to flip the whole scale because I want the children of the node to flip aswell

Because you are flipping the scale on the parent, all the children’s local coordinates and physics properties also get flipped.

But that’s exactly what I want, I want to be able to flip the parent so that the children get flipped aswell

When you flip the CharacterBody2D, you flip its normal left to right, that is why it goes back and forth. You don’t want to do that, and I believe messing with the physics bodies scales at all isn’t really recommended.

You should try individually flipping the children that you want to be flipped I think.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.