# Enemy Incorrectly Facing Player

### Godot Version

v4.2.stable.official [46dc27791]

### Question

I’m trying to have an enemy face the player when the player enters its detection box. But I’m having trouble with it. The enemy doesn’t always face the player (particularly when I’m trying to get it to face right after facing left).
Can anyone help me figure out what I’m doing wrong or link me to an answer for reference? I’ve looked around but couldn’t find anything.

• Here’s my code:
``````func _on_detect_box_body_entered(body): # Used when enemy DetectBox is entered
collisionX = body.position.x # Get player's position on collide
if body is Player: # Make sure it's the player
if collisionX > position.x: # This if statement is likely my source of error.
directionX = 1
scale.x = 1
elif not directionX == -1:
directionX = -1
scale.x = -1
\$Label.text = (str(collisionX - position.x) + " | " + str(directionX) + " | " + str(scale.x)) # Label is for debugging
``````

I found a function that was a holdover from the enemy script I used:

``````func flip():
scale.x = abs(scale.x) * -1
directionX *= -1
``````

And then I used it in my if statements (and modified that statements themselves):

``````func _on_detect_box_body_entered(body):
collisionX = body.position.x
print(collisionX)
print(position.x)
print("sgonose")
print(collisionX - position.x)
if body is Player:
if collisionX > position.x and directionX == -1:
flip()
#directionX = 1
#scale.x = 1
print("this is facing right...it better be")
if collisionX < position.x and directionX == 1:
flip()
#directionX = -1
#scale.x = -1
print("this is facing left...i hope so AAAAAAAAAAAAAAAA")
\$Label.text = (str(collisionX - position.x) + " | " + str(directionX) + " | " + str(scale.x))
#\$Label.scale.x = 1
``````

And now it works now!
Sometimes I think I should close my yap…

Node2D has a look_at() function you can use as well.

I’m aware of that function (and I’ve actually used it before!), but my enemy is a grounded enemy; it would look quite strange for the enemy to be rotated 45 degrees and thus not even be on the ground.