Following up on our post from a few months ago about how to create basic wander AI in Unity, this post will discuss in brief, the basics for setting up a simple follow AI. So let’s answer this question first:
What is follow AI?
If you’re reading this, you may already know what “follow AI” is but let’s look at some quick definitions first. AI as we know is defined as Artificial Intelligence however, it should come as a small warning that these days the term AI gets thrown around ALOT and in fact in many cases, the “Intelligence” part of AI is not actually all that smart (to paraphrase an old adage, a program is only as smart as its programmer). It has just become very easy to attribute AI as a buzzword to certain scenarios which use automation to some degree. Also, managers love buzzwords as well as the idea of AI and automation, but let’s not get into that very long digression. Let’s instead look at a simple follow AI construct for the purpose of video game development.
How to create a simple Follow AI
You may want to set up a Follow AI in Unity for a game or demo when you want an AI enemy or NPC (Non-player character) to seek you out, or (duh) follow you. Or maybe you want your AI character or object to find you and trigger a conversation in order to give you information or do you want to go the classic route where the AI finds you and attacks you? Or perhaps you want your AI to follow you and protect you, or even fight with you. There are as many reasons as there are solutions and when it comes down to it, the possibilities are endless. The choice is yours and there are no exact solutions.
Let’s look at some simple example code which is similar to what I used for my basic AI needs. The following code could be used as your AI controller script for example and then attached to your AI gameobject. What we’re doing here is using Transform to “look at” or notice the human player GameObject, which in this case is defined as “HumanPlayer”. Then, based on the distance between the AI object and the HumanPlayer, the AI will then “close the gap” between the two and move towards HumanPlayer, hence creating a simple AI follow effect. You can of course adjust the MoveSpeed and other variables to your liking and as I mentioned before, the possibilities are endless depending on what your game development goals are.
{ public Transform HumanPlayer; int MoveSpeed = 3; int MaxDistance = 20; int MinDistance = 1; void Start() { } void Update() { transform.LookAt(HumanPlayer); if (Vector3.Distance(transform.position, HumanPlayer.position) >= MinDistance) { transform.position += transform.forward * MoveSpeed * Time.deltaTime; if (Vector3.Distance(transform.position, HumanPlayer.position) <= MaxDistance) } }
We chose the following video tutorial from Press Start‘s YouTube Channel to illustrate further how to have an enemy follow you in a sort of 2D zombie game scenario. The technique is a little different from our example above and goes into much more detail. You can see how a similar technique can be applied to a zombie shooter or to something like a classic tank game. Unity is flexible enough that you can apply the same methods when building a 3D game.
One thought on “Unity: How to Create a Simple Follow AI For Your Game”