How do I make the enemy follow the player

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

PLEASE SOMEONE HELP ME, im losing my mental sanity with this, i just want to do an enemy, and for that i need the enemy to look at the player and follow the player, but every tutorial, or script dosen’t work, please someone help.

:bust_in_silhouette: Reply From: Dorcupi

It is actually quite simple. All you need to do is write a few lines of code.

var speed: float = 0.01 # put wanted speed here
look_at(player.global_position)
self.position = lerp(self.position,player.global_position,speed)

You may need to rotate the texture of the enemy but that should be all. Hopefully that helps!

I put that in the script, or i put it in a func, like i copy and paste the code but don’t work

blinkingcape | 2023-04-08 00:37

Put that in your physics proccess

Dorcupi | 2023-04-08 00:50

You put all that code inside your physics process function and then it should work.

Dorcupi | 2023-04-08 00:57

i put it but dosen’t work:

extends CharacterBody2D

func _physics_process(delta):
var speed: float = 0.01 # put wanted speed here
look_at(player.global_position)
self.position = lerp(self.position,player.global_position,speed)

blinkingcape | 2023-04-08 01:13

Make sure to add a variable called Player that links to your player

Dorcupi | 2023-04-08 02:12