Unity 2022 Nav Mesh 自动寻路入门
untiy 2022
window-PackageManager-AINavigation 安装 Install
2.创建一个空物体命名Nav,在其自身挂载 NavMeshSurface
然后点击bake 烘焙地形即可
3.创建palyer和怪物
怪物AI代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class MonsterAI : MonoBehaviour
{
public Transform player;
private NavMeshAgent agent;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
if (agent != null) {
agent.destination=player.position;
}
}
private void OnDrawGizmos()
{
if (player != null) {
Gizmos.DrawLine(transform.position,player.position);
}
}
}