Creating NPCs with Navigation Meshes and PlayMaker - part 4

Oct 11, 2019 at 10:00 am by nemirc

Adding AI to your NPCs could also be used for scenarios where you need to deal with friendly NPCs. If you have played Half Life, you may remember how cool it was when you could “lead” NPCs to different locations, and how they could help you do certain things. Granted that games nowadays do that frequently, but I consider Half Life the game that made others think more seriously about NPCs.

The following video shows how you interact with the NPCs.


How about we now do something like what we see in the video, and make our NPC follow/stop following when you interact with it? That can be easily done. First, create an agent like you did before (create a capsule, make it into an Agent, and also add the “eyes” so you know where it’s looking at). However, now you need to add an extra component: a sphere collider that is turned into a trigger.

The reason we add this trigger is to let the NPC “detect” when the character is close enough to give commands. In this case, the command is going to be “follow/stop following” me. Basically, this is what we are going to do: we are going to make the NPC detect when the player enters this trigger, and then, when the player hits the “interact” button (or whatever button we define to be the trigger of the command), the NPC will go into follow mode. Then, when in follow mode, if we repeat the process, the NPC will stop following. The plan is also to make the NPC receive orders only when the player is inside that trigger.

The next step is to create this FSM component on the NPC:


What this does is:

When the game starts, the NPC is on State 2. Then, when the object tagged as Player enters the sphere trigger (did you tag your player character as Player, as I’ve mentioned in previous parts?), it fires the “Trigger Enter” event and moves on to State 3. However, if the player gets away from the NPC, the “Trigger Exit” event is launched and the NPC reverts to State 2. Note that State 3 has a Get Button Down action, and when you press the required button (in my case it’s “InteractionA”), the NPC will move on to the following_intrigger state, where it is told to follow the player.

Note that State 2 doesn’t have the Get Button Down action. This is because we only want the NPC to be able to receive commands when the player is inside the trigger (in game, this would mean that the player is “close enough” to the NPC to give it a command).

In the following_intrigger state there’s another Get Button Down action that lets you revert the NPC to State 2 (making the NPC no longer follow the player). That following_intrigger state can also fire another “Trigger Exit” event that would make the NPC move on to following_outtrigger. Note that following_outtrigger lacks a Get Button Down action. Just like what I explained before, the idea is that you can only tell the NPC “stop following me” if you are close enough.

This approach is good when you only have one of these NPCs in any given section of the game, but can result in some problems if you are dealing with more than one NPC in that section. For example, imagine that you find yourself in a room where you have two NPCs. If you follow this method, you end up in a situation where both NPCs are following you.

In this case, you can do other things, like maybe adding the “command-giving” trigger to your player, and making sure the trigger is “floating” in front of your character instead of using a sphere trigger. This way, you make sure you only give orders to the NPC that is directly in front of you. The process is the same as above (only adjusted to make the FSM calls from another object). Go ahead and try to do it on your own.

Sign up for our newsletter

This website uses cookies to ensure you get the best experience possible More Info
Got it!