The NavMesh system is used to provide rudimentary pathfinding for enemies and NPCs in your games. It generates a NavMesh Grid based on the geometry you import into Unity. This information is noted from the Tutorial videos on the Unity homepage.
1: NAVIGATION OVERVIEW
Traditional pathfinding can be inefficient due to the complexity of the geometry in a scene. Unity’s NavMesh system can simplify this by baking a low poly mesh on top of the geometry, that scripted objects can much easier navigate. This can be used for players, enemies and NPCs. For example, in an RTS game, the player can order units to go to a certain point on the map. The NavMesh system is ideal for this. Alternatively, an enemy can be set to constantly move towards the player. The NavMesh system helps to determine where they can walk and what to avoid during this process.
– NavMeshes are managed in the Navigation panel in the Unity Editor. This can be accesed from Window > Navigation.
– Once a Nav mesh has been created, game objects wishing to utilise it should have a ‘NavMesh Agent’ component applied to it. For this tutorial, we refer to these game objects as agents.
– OffMesh Links can be used to allow objects to cross two separate meshes, or jump up/ drop down from meshes at different heights.
2: NAVMESH BAKING
– The act of creating a NavMesh is called Baking. To start this, open the Navigation panel from Window > Navigation.
– The navigation panel is separated into three tabs. Object, Bake and Layers. In order to generate a NavMesh, you must tell unity which objects are Navigation Static, i.e they dont move. This usually includes things like floors walls and large immobile objects. There are two ways to make an object Navigation Static. Select all of the objects you require in the hierarchy panel, then in the Objects tab of the Navigation panel, check the Navigation Static’ tick box. Alternatively, in the inspector panel, click the button marked Static’ at the top right, and ensure that static is checked from the dropdown list.
– Check the ‘Offmeshlink Generate’ box to automatically generate Offmesh links. See the section below fr more information.
– The Layers Tab is used to put different Navmeshes into different layers. These layers can have different ‘Cost’ values. The cost value determines how easy a mesh is to walk over, which the moving game object will take into consideration whilst pathfinding. For example, a road can be put in a layer with a value of 1, whilst wet mud can be put in a layer with a cost of 2, as it is more difficult to walk through. The character object will then try to walk on roads and avoid mud where possible.
– The ‘Bake‘ tab is where we can set the various properties of the Nav Mesh. Radius determines how close or far the NavMesh will be to walls, whilst height determines how high a ceiling must be for a character object to walk underneath it. Max Slope determines how steep a slope can be before it is considered impassible. Similarly, step height determines how high a step can be before it is considered an impassible wall.
The offmesh settings are discussed below, and the advanced settings are usually ok to leave untouched. Note that choosing to generate a heightmap will make your mesh more accurate, but slower to generate.
-Once all settings have been made, click the Bake button to generate the NavMesh, which will appear on screen as a blue grid. Note that if the blue grid doesnt appear, check the ‘Show Navmesh’ box in the scene view window.
3: THE NAVMESH AGENT
– In order for a character or gameobject to navigate a NavMesh, it needs a Navmesh Component applied. It also needs a target to follow. For example, his target can be a gameobject held as a variable in a script.
– To add a Navmesh Agent component to a game object, in the inspector panel choose Add Component > Navigation > Nav Mesh Agent. The Navmesh Agent has a number of properties that can be adjusted. The Radius is the size of the Agent, used for determining how narrow a walkway the object can traverse. Speed is how fast the agent can move. Angular speed is how quickly the object can turn, and Stopping Distance is how far from the target that the agent will start to decelerate. Auto Braking will revent the agent from overshooting its target, whilst Auto Repath will allow the agent to rebuild Its path should the current one become invalid (i.e if an obstacle gets in the the way, or if the target moves.) Height determines the height of the agent. Obstacle Avoidance Type is a range between high quality and low quality, with a trade off between accuracy and efficiency. Avoidance Priority determines which agents have priority when navigating. Agents with a lower value have higher priority than those with a higher value. ‘NavMesh Walkable’ determines which layers of the Navmesh the selected Agent can walk on.
– To make the agent move towards it’s target, we must add a few variables and functions to a script, as shown below:
public Transform target;NavMeshAgent agent;Void Start (){agent = GetComponent<NavMeshAgent>();} Void Update() { agent.SetDestination(target.position); }
4: OFFMESH LINKS
OffMesh Links are pathways that connect separate pieces of NavMesh, so that Agents can traverse them. There are two ways to create links – generate them automatically, or place them manually.
– In creating links, we must first select the two static objects we want to create a link between (platforms, crates etc) and check the Generate OffMesh Links button in the objects tab of the Navigation Panel. Next, we must go to the Bake tab, and set the Drop Height and Jump Distance to acceptable values that the agent AI can navigate. You will also need to disable the height mesh property, if it isn’t already. Just hit bake, and Unity will generate the links just like it generated the mesh. Note that You can have jump and Drop links coming from the same spot, and the AI will know which to use when pathfinding. Finally, the Cost Offset property is important as it helps the AI determine that jumping off or across a ledge will be fast than trying to go around. As such, this should always be a negative number.
– To make links manually, we must make two empty game objects – one for the start and one for the end of the link. We mus then add a Offmesh Link component to one of the objects. For handiness and ease of finding, it is good practice to add it to the start object, even though technically it doesn’t have to be a part of either – it can be attached to a separate object altogether. In this component, you must add the start and end objects to the necessary slots. The link can be set to bi directional, so that the agent can jump back across platforms, and can be toggled as active or not. You can choose which layer the link is part of, and also toggle whether the link will be recalculated if any of the objects move for any reason.
-NB Note that manually set p links do not require the NavMesh to be rebaked.
-NB Note that agents will automatically know to use offmesh links if they spot them and are the most efficient way to travel.
5: NAVMESH OBSTACLES
Unlike static objects that are baked into a NavMesh, NavMesh obstacles are dynamic obstacles that can move, such as doors or even other NPCS.
To turn any object into a NavMesh ostacle, go to Add Component > Navigation > NavMesh Obstacle. The Radius and Height properties determine the size of the obstacle, and will usually be adjusted similar to the size of the objects mesh. The Carve option is interesting. When selected, it cuts a chunk out of the NavMesh, meaning agents wont try to walk over it when pathfinding, but instead find another route. If carve is unselected, the agent may try to walk through the obstacle, but will be blocked all the same. Move threshold is only used when carve is selected, and determines how far the obstacle object must move before its affect on the Navmesh is recalculated.
コメント