World's script:
func _process():
var path_go = $Navigation2D.get_simple_path($Enemy.position,$Player.position)
$Enemy.path = path_go
Enemy's script:
func _process():
velocity = speed * delta
while velocity > 0 and path.size() > 0:
var distance_to_target = position.distance_to(path[0])
if velocity <= distance_to_target:
position += position.direction_to(path[0]) * velocity
$Player_animation.play("run")
else:
position = path[0]
path.remove(0)
velocity -= distance_to_target
I'm making the enemy find the player themselves, but they know to dodge obstacles by themselves, using Navigation2D which is a child of World and NavigationPolygonInstance which is a child of Navigation2D. According to my code above, it's pretty good. But there is a problem is how and where should I add the line of code so that when the Player goes behind Enemy, Enemy knows to turn around. Don't use Flip, but use Scale.x so it can bring collisions. So how do I insert the code and where do I insert it to do it all? And I want all the characters of the Enemy group to have to find the Player when they are created in the World. Because according to my code I can only make one enemy find the Player and dodge the obstacles. And if I want all the Enemies in the Enemies group to simultaneously look for the Player, the above code doesn't seem to work. Someone please help me. Thank you very much.