Hello again, I've got an enemy sprite that I want to flip horizontally and have it look at the player as it's chasing them.
I'm using a CharacterBody2D for the enemy and a lerp to change position.
The enemy ends up turning upside down when looking at the player (see image).
So I have 2 problems here:
- how do I stop the enemy from turning upside down and just flip horizontally when chasing the player
- is there a way to make the enemy move so I can detect its velocity? (see below for more info on this)
Code for moving and looking at player, I'm using:
func chase():
await get_tree().create_timer(2).timeout
var lerpSpeed = 0.01
if player and position.distance_to(player.position) > 20:
look_at(player.position)
self.position = lerp(self.position, player.position, lerpSpeed)
PS: One way I solved the issue was that instead of using look_at() I used this:
func lookAt():
if player and player.position < position:
$Icon.flip_h = true
else:
$Icon.flip_h = false
But would still like to know if there's a better way to do it 🙂
Problem number 2:
I tried to use move_and_collide() but that doesn't affect the enemy's velocity either.
This is just so that I can set the correct animation when it's moving with:
if velocity == Vector2.ZERO:
animationTree["parameters/conditions/idle"] = true
else:
animationTree["parameters/conditions/idle"] = false
animationTree["parameters/conditions/isMoving"] = true