Enemy following the player in 2D Top Down

I’m trying to make enemies follow the player on a 2D Top Down on two diffrent projects, on the first it worked but It ran away from the player while on the second it didn’t work at all

My current code:
extends CharacterBody2D

var speed = 7
var player_chase = false
var player = null

func _physics_process(delta):
if player_chase:
position += (player.position - position)/speed

func _on_detection_area_body_entered(body):
player = body
player_chase = true

func _on_detection_area_exited(body):
player = null
player_chase = false

I think you follow the same thing as this person and might have the same problem

1 Like

I think you should use a Pathfinder instead. Saves you quite some time, if you just use a tilemap

1 Like

I tried using the same code, but when I tried to test it the project crashed and those error messages appeard:
image
image

That wasn’t the solution though.
Use your old code and try resetting the sprite and collision transform.position of both the player and enemy scene to 0.0

How exactly can I do this?

go to your player scene and enemy scene
check your sprite and colision
find transform.position in the inspector and set it x and y to 0

The Player’s X and Y were already on 0 while the Enemy had X on 0 and y on 0.5
I set it to 0 but nothing changed and the enemy still isnt following the player

can you show the player scene the enemy scene and the node tree
make sure you set the sprite and collision and not the player node

Sure
Player:
image
Enemy:
image
node tree
image

  1. click on AnimatedSprite2D
  1. on your right scroll down to find or type in “position”
  2. if it isn’t 0.0 reset it to 0.0
  3. your sprite should have a cross on top of it

It already was in 0.0

do the same for the enemy

Still doesn’t work