- Home /
AI Patrolling
I have created an enemy to follow the player whenever the player is within range. But after the player went out of range, the enemy does not move back to its own default position for patrolling.
I have 1 questions here, 1. How to create a patrolling AI? 2. Can I know how to do set the enemy to go back to its default position for patrolling?
Thanks!
I know what's a state machine, but I do not know how to implement in Unity C#. Thanks for the info.
Isn't this a matter of checking if the AI is still following the player and if it isn't, send it back to the patrolling path? In which case you would just have to make sure that the AI can find the way back assu$$anonymous$$g that there might be obstacles in the way which would mean that you have to use pathFinding.
ShadoX, do you have any idea how do I program for the AI find it's way back?
You could try checking out http://unity3d.com/unity/quality/ai, http://arongranberg.com/astar/features or http://hutonggames.com/playmakerforum/index.php?topic=1975.0
I'd suggest to simply look for some pathFinding solutions for Unity. Those are just 3 that I found after a couple of seconds, but I'm pretty sure that usually when you do look into pathFinding that you most probably will end up finding just a couple of algorithms anyway.
Sorry for not providing more info, but I haven't touched this subject yet including those algorithms.
Answer by RyanZimmerman87 · Sep 19, 2013 at 02:41 AM
A really easy way to do this is to create a vector 3 position for the enemy on Start()
If the enemy is no longer following the player simply turn them around back to this position. If the enemy is patrolling too far from this position simply turn them around back to this position. That's probably the simplest way to do this.
So whenever you want them to return to the start point whether it be because the player is out of range or they are too far from the start point or any other variable you want, you can use:
transform.LookAt(enemyStartingVectorPosition);
And then have the enemy move in this direction. If you want them to follow specific patrol routes or navigate around walls it will require additional logic.