How to create NPCs with Navigation Meshes and PlayMaker - part 2

Sep 24, 2019 at 10:00 am by nemirc

Using PlayMaker to make an NPC

Last time we used PlayMaker to make an NPC that would "patrol" between a series of different points, and then we also made an NPC that would follow the player. Right now we will explore how Off Mesh Links work.

Many NPCs in video games are limited to moving on the ground. But, what happens if you want an NPC to climb onto a table or something like that? For example, maybe your NPC is a cat, and you need the cat to jump onto a table. You can do that easily in Unity using Off Mesh Links.

Off Mesh Links in Unity are navigation elements that serve to tell an NPC that it has to "jump" from one side of the link to the other.

In Unity, they are seen as a double arrow connecting two circles. To create an Off Mesh Link, first create the object the NPC should jump onto (say, a cube representing the table) and then add an Off Mesh Link component. The component has two empty slots where you must assign a start and end position. To make it easy, simply create two empty game objects as children of the cube, move one to the upper surface of the table, and move the other to the floor level, and assign them as the Start and End of the link (it doesn’t matter which one is the start or end, since you are making the link by-directional).

Now, let’s make an NPC like we did last time, but the PlayMaker FSM we will make is going to be different: we will simply make the NPC go to a specific goal on top of the cube we created. The final setup looks like this:

If you press play, the NPC should move towards the cube and then "jump" onto the cube. If you look closely, the NPC doesn’t really "jump" onto the table, meaning, it doesn’t follow the "parabolic movement" associated with a jump. It simply "flies" towards the other side of the link. The easiest thing you can do to solve this is to counter this look with an animation (adding extra vertical movement to the animation so it looks like it’s actually following the parabolic movement).

As you can imagine, you can use this same technique to make the NPC jump across multiple platforms.

Off Mesh Links are not only used to allow NPCs jump onto different surfaces. They are very useful when you are working with level streaming. Level streaming is a technique used in game development that allows you to load small parts of a big world as they are needed.

This is very useful in open world games, for example, since you may have a really big map comprised of small areas. Rather than loading the full map, you only load the area you need (for example, a small town), and loading/unloading areas as you need them.

However, level streaming can also be useful for games taking place in closed environments (like a house, or a space station). In these cases, levels can be a room or a collection of rooms that go together. If you add a navigation mesh to every room (level), when they are streamed into the main game you end up with a lot of different navigation meshes instead of a single one.

In these cases, you can use Off Mesh Links to connect the two navigation meshes from the two different levels (on a side note, this is a technique I’m using for a game I’m working on, hence why the following two images show complete environments). The image below shows an example of this technique. In this case, each room (marked 1 and 2) is a separate level, with its own nav mesh, and they are streamed when the player opens the doors.

As I was explaining before, you need an Off Mesh Link to join both Nav Meshes together, since they are separate. By doing this, an NPC in room 1 would be able to go to room two. The image below shows the setup.

And this is it for now. Next time I will show you other things you can do with NPCs, to make them a little more interesting.




Sign up for our newsletter

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