0 votes

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?

in Engine by (16 points)

3 Answers

+1 vote

Use KinematicBody2D and moveandcollide() to move the object along the path and when the player collides with it. the moveandcollide() 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.

by (271 points)
+1 vote
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"
by (20 points)

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 lastposition 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 lastoffset 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...

0 votes

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.

by (16 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.