Simple navigation wayfinding function
Selected as Static Grid Navigation
Windows======>Ai========>Navigation, click Bake
Hang a navigation grid component on the character
Hang up the script and set the mouse click position as the end point of the navigation
using UnityEngine;
using UnityEngine.AI;
public class PalyerMovement : MonoBehaviour {
private NavMeshAgent agent;
void Start()
{
//获取角色上的NavMeshAgent组件
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
//鼠标左键
if (Input.GetMouseButtonDown(0))
{
//射线检测
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
//hit.point射线触碰的Position
//SetDestination设置下一步的位置
agent.SetDestination(hit.point);
}
}
}
}
Navigation panel parameters
baking
There is a cost behind the area, which is the weight when choosing the optimal route.
Although the following route is close, it has a high weight.
Effect of Jump Height and Distance
dynamic obstacles
Navigation grid proxy component, automatic braking
Dynamic blocking component, dynamically set obstacles and check the cutting component
Split grid jump lines
Used to perform navigation grid links on grid surfaces that cannot be walked directly, such as jumping from buildings, jumping into ditches, passing through after opening the door, etc.
Jump from one point to another
The ground in the scene needs to be checked
Set start and end points
Dynamic baking of navigation meshes at runtime
Copy the following folder to the project and use it
Needs to be re-baked
Right-click to generate a cube on the map and render it
using UnityEngine;
using UnityEngine.AI;
//右键在场景中新建一个Cube,重新烘焙导航网格
public class BuildNavMesh : MonoBehaviour {
public GameObject buildPrefab;
private NavMeshSurface surface;
void Start ()
{
surface = GetComponent<NavMeshSurface>();
}
void Update ()
{
// 鼠标右键点击
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
GameObject go = Instantiate(buildPrefab, hit.point, Quaternion.identity);
go.transform.SetParent(this.transform);
// 生成完之后立即重新烘培场景
surface.BuildNavMesh();
}
}
}
}
You can also add a navigation grid component to the prefab and drag it into the scene to bring the grid component.