How to stop an object from following a path and then restart it later?

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

I have an enemy that patrols along a path2d. When it collides with the player, I want it to stop following the path and begin to chase the player. When the player leaves the enemy’s detection area, I would like it to go back to the position it was on before chasing the player.

Do I simply need to remove the enemy as a child of the pathfollow2d node and then later re-add it as a child?

:bust_in_silhouette: Reply From: spike59
i think you could use some states to manage the behaviors of your enemy

like this:
enemy
  life
  alive
  damage
  last_patrol_pos
  return_path

process(delta)
 if state == "patrolling"
   do patrol stuff
   folow path
   if player detected
      last_patrol_pos = position
      state = "chasing"
 elif state == "chasing"
  do chasing stuff
  if player not_detected
     state = "end_chasing"
     return_path = nav2d.get_simple_path(position,last_patrol_pos)
  else( state == "end_chasing")
     follow path(return_path)
     if path ended
        state = "patrolling"
    

This is more or less what I had, my problem was that in order to get the player to stop following the path and do the “chasing stuff”, it can’t be a child of the original pathfollow node (unless I’m missing something).

My solution to this in the end was to remove the enemy node as a child of the follow2d node, reparent it to a node higher up in the tree, do the chase stuff

Then when it’s lost track of the player, as you suggested, move the enemy back to the last_position it was at while on the path, re-add it as a child to the follow2d node, set the offset of the follow2d node to the last_offset it was at before chasing the player, and then reset the enemy to position(0,0) in order to make sure it was following the path exactly.

If anyone’s got insight into why I might be doing this a bad way, I’m all ears. removing and readding the enemy as a child seems wonky…

notsmith | 2020-09-01 23:00

:bust_in_silhouette: Reply From: Xian

Use KinematicBody2D and move_and_collide() to move the object along the path and when the player collides with it. the move_and_collide() would return the player’s data. Using that data you can make it stop moving when it has the player data or move when it doesn’t have it. You can also use the returned player data to get the coordinates of the player, allowing you to easily set the object to find and chase after the player.

:bust_in_silhouette: Reply From: notsmith

described my method of solving this in a response to spike59’s answer.
Please let me know if there’s a preferable way of doing this!

Xian’s method seems all around better than mine.

i’m new to godot and i dont’ know the better way to do things, i learned about paths with gdquest, you can look the video for pathfinding, i use it on my game and my character change path whenever i want just by setting a new path , (by clicking somewhere or when a task is added )but i never used the “path follow2d stuff” so sorry don’t want to send you in bad direction
hope someone can help you !
for information this is the video for pathfinding(but no follow2d)
https://www.youtube.com/watch?v=0fPOt0Jw52s

spike59 | 2020-09-03 14:58