Navigation and Wayfinding
navmesh
First create a simple scene in Unity.
Add the ball to the Nav Mesh Agent component, find AI in Windows and add Navigation (navigation) , and Navigation will appear .
Select all static objects, check theNavigation StaticClick the Bake button on the Bake interface. A navigation grid appears under the Scene view.
Introduction to Navigation
Object
Attributes | Function |
---|---|
Scene Filter | scene filter |
All | set all objects |
Mesh Renderer | Set up all objects except camera and light |
Terriain | Only set the terrain |
Navigation Static | When checked, it means that the object participates in the baking of the navigation grid |
Generate OffMeshLinks | After checking, you can jump (Jump) and drop (Drop) in the navigation grid |
Navigation Area | navigation area |
Bake
Attributes | Function |
---|---|
Agent Radius | Sets the radius of the object with the navmesh proxy, the smaller the radius, the larger the area of the generated mesh |
Agent Height | Sets the height of an object with a navmesh proxy |
Max Slope | Set the slope of the maximum allowable reachable slope |
Step Height | Set the height allowed to reach the steps |
Drop Height | Set the maximum drop distance allowed |
Jump Distance | Set the maximum jump distance allowed |
Manual Voxel Size | Set whether to manually adjust the baked size |
Voxel Size | Set the unit size of baking to control the accuracy of baking |
Min Region Area | set minimum area |
Height Mesh | Sets whether to generate exact rather than approximate baked effects when the terrain has drops |
Agent
Attributes | Function |
---|---|
Name | The name of the object with the navmesh proxy |
would laugh | the radius of the object |
Height | the height of the object |
The code controls the movement of the object ball
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ray : MonoBehaviour
{
NavMeshAgent agent;
public Transform red;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从相机发射一条射线到点击处
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
//如果点击场景中一点则小球通过计算从最近的距离移动到这一点
agent.SetDestination(hit.point);
red.position = hit.point;//点击处出现红色小球标注便于观察
}
}
}
}
NavMeshAgent component
1. Steering [operation]
Speed[speed]: the moving speed of navigation;
Angular Speed[turning speed]: the model is the Z-axis towards the target point; if not, turn;
Acceleration[acceleration]: keep the default;
Stopping Distance[stopping distance]: the distance from the goal Stop when the ground is too far;
AutoBraking [automatic stop]: Keep the default ticked state.
2.Obstacle Avoidance
Radius[radius]: controls the radius of the "cylinder" of the agent, that is, the size; Height[height]: controls the height of the "cylinder" of the agent; Quality[quality]: the quality of obstacle avoidance
. If you have a large number of agents, you can save CPU time by reducing the obstacle avoidance quality.
priority [priority]: When performing avoidance, this agent will ignore lower priority agents. The value is in the range 0-99, where lower numbers indicate higher priority.
3.Path Finding
Auto Traverse OffMesh Link is set to true to automatically traverse the grid outside the link (Off-Mesh Link). You should turn this off if you want to use animation or some specific way to traverse out-of-grid links.
Auto Repath When this property is enabled, the agent will try to pathfind again when it reaches the end of a partial path. When there is no path to the goal, a partial path is generated to the closest reachable location to the goal. Area
Mask The Area Mask describes the type of area the agent will consider when pathfinding. Each mesh region type can be set when preparing the mesh for navmesh baking. For example, you can mark stairs as a special area type and prohibit certain character types from using them.