Creating NPCs with Navigation Meshes and PlayMaker - part 5

Nov 01, 2019 at 10:00 am by nemirc

As I said in the previous part of this series, AI-driven NPCs can be used for scenarios where you need to deal with friendly NPCs.

Last time I showed you this clip from Half Life and how you interact with the NPCs, and then I explained how to make an AI that will start/stop following your player character.

In Half Life, NPCs will also walk to an "interest point" to do something, if they are following you, like shown in the first minute of this video.

You can do something similar for different scenarios, like making an NPC go to a device open a door, or to start a machine.

To do this, first add a tag to your NPC (in my case I am using the tag "stalker" because it was already available in my project, and I didn't want to create a new one). Then, we are going to create the "device."

The "device" will consist on two objects, a big cube, and a small cube. The small cube is going to serve as the destination for the NPC, meaning, the spot where the NPC should stop. First, create the objects as shown on the image (remove the collisions on the small cube).

Now, add a sphere collider to the big cube on the wall, and turn that sphere collider into a trigger. The idea is to make the NPC detect the trigger so it knows when to go to the device. You can also do it by measuring distances (walk to the device if the NPC is closer than X amount of units), but I think using the trigger uses less calculations.

And now, create this PlayMaker FSM:

The idea is simple. When the NPC enters the device's trigger (detected by the NPC's tag) it's going to set the device's goal (small cube) as a Game Object variable in the NPC's movement state machine, and then launch an event called DEVICE (I'll explain why I'm using all caps in a bit).

Now, we have to modify the NPC's "follow" FSM like this:

Only the part inside the blue square is new (everything else remains the same as before). This is what it does: first, it sets the new stop distance at "0" so it stops right at the small cube (this is why removing collisions from the small cube was important), and then it sets the NPC's new goal as the variable set by the device's FSM. Then, we get the parent of the small cube because, as soon as the NPC reaches its destination, it should face the big cube on the wall. Then, the last one only has a comment, but in that last state you should add whatever actions you need to perform (open a door, call another event, etc.).

As you can see, the event name DEVICE is written in all caps. I do this mostly as a visual aid. You can see the DEVICE event is set up as a global event. That way, the events being used as global events are easier to find in a bigger FSM.

Now, after doing this, the NPC will simply stop and you can't make it follow you again. You can always reset the FSM after the scripted sequence (that the NPC would perform after reaching the device) is finished.

And that's it for this series on working with navigation meshes and NPCs. While I am stopping the series here, that doesn't mean I won't write anymore about AI-driven NPCs in Unity. Next time, however, it will be to make a more specific "how-to," like "how to make a friendly NPC in a shooter game" or "how to make a pet NPC."

Sign up for our newsletter

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