一、搭建一个场景
二、设置不可走区域
三、烘焙不可走区域
点Bake按钮执行烘焙。
四、烘焙可走区域
点Bake按钮执行烘焙。
五、编写以下脚本并挂到主角对象上。
主角上添加组件NavMeshAgent 。
- using UnityEngine;
- using System.Collections;
- public class NavMeshAgentTest : MonoBehaviour {
- public NavMeshAgent agent;
- Vector3 point;
- Ray aray;
- RaycastHit ahit;
- void Update () {
- if (Input.GetMouseButtonDown(0))
- {
- aray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(aray, out ahit))
- {
- point = ahit.point;
- }
- agent.SetDestination(point);
- }
- }
- }
六、运行测试
鼠标点地面,小球会自动绕过建筑移动到鼠标点击点。