How do I make an AI enemy turn around?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rubin

Hello! I’ve been working on a simple enemy for my game that follows a path (using a navigation mesh) to reach and attack the player. The enemy is moving nicely, however it doesn’t rotate (and can’t attack the player from all directions since it only attacks forward).

I know it’s more of a math question than programming itself.

:bust_in_silhouette: Reply From: USBashka

The easiest way to do it is look_at() function. Add it to the _physics_process of the enemy’s code and it will constantly rotates to the player. Like this:

func _physics_process(delta):
    look_at(player.translation, Vector3.UP)

It only works if enemy faces to the -Z axis.