unity中的 Navigation AI使用
1、在unity中安装 AI Navigation


2、在scene中添加一个Plane,在 Plane中添加一些 Cube用于测试导航。

3、设置不同的导航模式


4、烘焙不同的导航路径,便于实际中使用。

5、添加一个物体,用来测试自动导航。

6、PlayerController.cs脚本编写。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class PlayerController : MonoBehaviour { private NavMeshAgent navMeshAgent; // Start is called before the first frame update void Start() { //获取代理组件 navMeshAgent= GetComponent<NavMeshAgent>(); } // Update is called once per frame void Update() { Debug.Log(navMeshAgent.isOnNavMesh); if (navMeshAgent != null && navMeshAgent.isActiveAndEnabled && navMeshAgent.isOnNavMesh) { ///获取鼠标左键 if (Input.GetMouseButton(0)) { //获取点击位置 Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray,out hit)) { //点击位置 Vector3 vector=hit.point; //设置目标点 navMeshAgent.SetDestination(vector); } } } } }
7、把脚本挂在到物体上开始运行项目。

大致就是这样子,里面的有些参数细节,可以自己边练习边摸索,反正我也是在摸索。
浙公网安备 33010602011771号