wiki.sine.space | sinespace

Navigation bake

From wiki.sine.space
Revision as of 05:41, 17 January 2019 by HannahT25 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The NavMesh function in Unity can help you define your region where is walkable and where is not, so if you have an auto-walk function for player or NPC, it can automatically calculate the routes.

If you are interested in how to fully use the NavMesh function, please check official tutorial of Unity.

Here, we will only introduce some basic methods.

As an example, I created a region with some simple objects as obstacles.

Global.jpg


Create a NavMesh Map

First, Click the menu Window -> Navigation to call out the Navigation window(In Unity 2018, it's in Window -> AI -> Navigation). There are four tabs in Navigation Window: Agents, Areas, Bake and Objects.

The Agents tab is a place to record profiles for different parameters, when you attach Nav Mesh Agent component to a character, you can choose the agent profile you set in the agents tab.

Agents.jpg

The Areas tab is for define the layers of navigation. There is already 3 built-in layer: Walkable, Not Walkable and Jump. You can always add new layer if you need. The Cost defines whether the layer is hard to walk or not -- more cost, much harder. Normally, the Auto-walk will always choose a path with lower cost.

Areas.jpg

The Object tab is to set the navigation parameter for the objects in the scene. The 3 filters, All, Mesh Renderers, Terrains can help you to find the objects, and if we need to set an object walkable or not, pick the checkbox Navigation Static, then choose its layer. If you want set it jumpable, for example: jump across a gap between two platform, of jump down from somewhere high, you should also pick the Generate OffMeshLinks checkbox then choose the Jump layer for Navigation Area.

Object.jpg

The Bake tab includes all detail settings here. The Agent Radius defines the width of character and how narrow the path it could pass. Agent Height stands for the height of character, so if there is a height limitation area, the character shall not pass. Max Slope defines the maximum angle the character can move on, and objects lower than the step height will not be considered as an obstacle. Drop Height is the maximum height which the character can jump off, and jump distance is a horizontal distance which the character can jump across.

Bake.jpg

When all the settings are done, you can click the Bake button to generate the NavMesh Map. When it's complete, the Scene window will display the Navmesh map.

NavMesh.jpg

The NavMesh jumpable with off-Mesh links will have an arrow and circle like this:

Jumpable.jpg


Create a NPC moves in control of NavMesh

You will need a lua script to set an object moving through the NavMesh. Here, I'll show you a simple example.

First, create a capsule as the character we want to move in the baked scene, then add the component "Nav Mesh Agent" to it.

NavMeshAgent.jpg

Choose the profile you set in the Navigation window, and set other parameters as you want or just leave it as default -- if you wonder each parameter is for, check the link of tutorial above.

Create an empty object and move it to the destination you want to set for the move. Change the object's name to something easy to find in script, like here, I name it as "dist".

Dist.jpg

Now add the lua script to the capsule. Add the "Scripting Runtime" component to it, then write the code like the image below:

NavMessScript.jpg

The line "Space.log(agent);" isn't necessary, it's only a log to make sure the script is working.

You can add an clickable activator to active this script when click the capsule, or it will start moving automatically.

NavMeshGIF.gif

Here is a GIF which shows you the result of NavMesh move.