How do i stop my enemy from going into me and instead keeping a certain distance when attacking?

Godot Version

4.2.1 Stable Mono


So as the title says I’m having problems with the my enemy going to close to the player

This is the code that moves the enemy and makes it look at the player:

If you don’t want the slime to rotate towards the player why are you using LookAt()?

My dumbass didn’t think when I was posting this, I apologize. I have changed the title now

You could just store the distance and use it to liit the movement, or you could use collider bodies.
The former is very simple.

float distance = (_character.GlobalPosition - GlobalPosition).length()
if (distance > 10.0)
	MoveAndCollide(dir * Speed * (float)delta)

And you can make that 10.0 a variable and export it or make it whatever you want.
For the other option of setting colliders, refer to the Physics2D tutorial in the manual.

1 Like