Unity includes a set of components that let you create computer-driven NPCs that you can use for various things in a game, from characters with pre-defined movements across the map to fully functional enemies. This time I want to share some tips to set up agents using PlayMaker. On a side note, I might be reaching the point when I need to move away from PlayMaker and delve into coding, since my PlayMaker state machines are becoming larger and larger, making things hard to manage.
For this first example, we will create an NPC that moves to different goals. The idea is simple: the NPC will move to a goal and, when the goal is reached, a new goal will be picked from a list. The NPC will then move to the next goal, and the process will repeat.
This can be useful if you are making a game where you have various NPCs that simply need to move from one point to another without anything else happening, meaning they will not react if you are close, or they will not interrupt their movement for any reason.
The first thing you need to do for this (or any other example) is to create a Navigation Mesh. Navigation Meshes tell your Unity agents which parts of the level are walkable and which ones are obstacles or walls. To do this, first you need to open the Navigation pane (after your level is created, of course, since you can’t add a Navigation Mesh to an empty scene), configure your desired settings, and then bake. For this exercise I will use a prototype level for one of my current projects.
This is how the baked Navigation Mesh looks like on my level. The blue areas are the areas where the NPC can walk.
Next, you need to create the goals the NPC will move to. If you see the image below, you will see I have created 3 spheres that I will be using as goals. These spheres are turned into triggers (more about this soon).
Now it’s time to create the agent. Right now, we only need a cylinder or a capsule (I created a capsule). Create the object and then add a Nave Mesh Agent component. You can leave the default settings since we are not worrying about fine-tuning movement right now. Then, create a PlayMaker FSM on your capsule, and create the next state machine:
The state machine does the following: First, it waits for one second before it starts to move the agent. Then, it picks a random goal object from an array (the array was created manually by me, and I added the 3 goal objects I created above), and then it sets the Agent’s destination as the selected object.
If you click the Play button, you will see the Agent starts moving in one direction, towards one of the goals. However, if you stop the game and play again, the Agent might move to another goal, since they are picked at random on runtime. However, when the Agent reaches the goal, it simply stops, which is not what we want.
Now, what we need is for the NPC to pick another goal after it reaches the current goal. I found the best option was to add a PlayMaker State Machine to the goal themselves, and this is why the goals had to be triggers. The State Machine created on each goal is the following:
What it does is this: it detects when the NPC has reached the goal (by using a trigger collision). When the NPC enters the trigger, another event is sent, and we pick a random object from an array (just like before), except that this array only contains the “other” goals. For example, say that my goals are named goal1, goal2 and goal3; the PlayMaker FSM added to the object goal2 will have an array that only includes goal1 and goal3, while the FSM on goal3 will have an array that only includes goal1 and goal2. This way, we make sure the game doesn’t pick the same goal it had already picked. Lastly, this FSM sets the NPC goal as the new game object, effectively making the NPC move to the next goal. When the NPC reaches the next goal, the cycle repeats and a new goal will be picked.
If you press Play after doing this, the NPC will move to its first goal, then to another one, and another one, indefinitely, until you press Stop.
Another example I will show you today is an NPC following a character. This has different usages, actually: the most obvious is an enemy following the player. However, it could also be used to create an NPC that must follow the player for any specific reason. To do this, create another capsule that could serve as the Player, and tag it as “Player,” and then, create a new Agent and add this simple FSM to it (you could use the same Agent, but you will need to turn the already-created FSM first, since they will keep giving your Agent contradicting goals).
This FSM first looks for your Player by looking for the object with the tag Player (hence the previous instruction), and then assigns it as the Agent’s goal. If you press Play, you will notice the NPC moves to your Player. However, it bumps into it. This is because the stopping distance is set to 0. If you set your stopping distance to a higher value (say, 2), it will stop 2 units away from the Player. If the Player moves, the NPC will continue chasing the Player.
That’s it for today. Next time, I will show you how to do other things, like make an NPC “jump onto” another object.