How to flip enemy scale

Godot Version

<stable4.2>

Question

<how do you flip enemy scale properly? Im trying to make enemy to look at player side during CHARGE state. but when player is at right hand side of enemy, enemy flip goes off.

extends CharacterBody2D

var speed = 10
var gravity = 500
var direction = 1
@onready var player = get_node("../player")

enum States {IDLE, AIR, CHARGE, THROW, DEATH}
@onready var state: States = States.IDLE

func _physics_process(delta):

	match state:
		States.IDLE:
			print("IDLE")
			if !is_on_floor():
				state = States.AIR
			$AnimationPlayer.play("idle")
			velocity.x = speed * direction
			velocity.y += gravity * delta
			move_and_slide()
			if is_on_wall() or not $RayCast2D.is_colliding():
				direction *= -1
				scale.x *= -1
			if player_in_sight and $CoolDownTimer.is_stopped():
				state = States.CHARGE
		States.AIR:
			if is_on_floor:
				state = States.IDLE
			$AnimationPlayer.play("idle")
			velocity.y += gravity * delta
		States.CHARGE:
			print("CHARGE")
			$AnimationPlayer.play("charge")
			velocity.x = 0
			velocity.y = 0
			if player.global_position.x < global_position.x:
				scale.x = 1
			else:
				scale.x = -1 
		States.THROW:
			print("THROW")
			$AnimationPlayer.play("throw")
			velocity.x = 0
			velocity.y = 0
			$CoolDownTimer.start()

ezgif-5-3d214198af

You need to use flip_h.
flip_h is available on Sprite2D and AnimatedSprite2D nodes.

if player.global_position.x < global_position.x:
      your_sprite_node.flip_h = true
else:
     your_sprite_node.flip_h = false

that didnt solve the problem…

I want to make it look at player during charge state. and else state just look at direction of where its moving to

What does the charge animation look like?

i think im getting close to solve the problem. but still, sometimes it works sometimes it wont. very frustrating.

func flip():
	if player.global_position.x > self.global_position.x:
		print("player on right")
		scale.x = -1
		direction = 1
	if player.global_position.x < self.global_position.x:
		print("player on left")
		scale.x = 1
		direction = -1

You can’t flip the x scale, see the documentation, you need to use the flip property like suggested above

but i need to flip ray cast as well so enemy cant also flip when at edge

Then you need to rotate those instead because of this limitation

func flip():
	if player.global_position.x > self.global_position.x:
		print("player on right")
		$Marker2D.scale.x = -1
		
	if player.global_position.x < self.global_position.x:
		print("player on left")
		$Marker2D.scale.x = 1

I added sprite and ray cast under Marker2D.
How do you get the enemy back to looking in the direction they were originally looking during idle state?

but then why do all people on thread asking about how to flip enemy says to change x scale

Because they aren’t aware of this limitation, or they use 3D perhaps

Why not use 2 raycasts?
On for the left side and on for the right side and set enabled true/false.

Or change the raycasts target position to the move direction.

1 Like