Unity实现自动寻路

要点:
1、调出导航窗口:
unity3d 顶部菜单 → 窗口(window) → AI → 导航(Navigation)。
会弹出导航窗口,一般在检查器(Inspector)窗口右侧。

3、实体对象添加自动寻路组件:
选择实体对象 → 检查器窗口(Inspector)→  添加自动寻路组件(Nav Mesh Agent)→ 添加控制脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.AI;   //自动寻路 AI ,头文件

public class KillerCtrl : MonoBehaviour
 {
        NavMeshAgent agent;  //自动寻路AI代理
         GameObject obj;

 void Awake()
 {
         agent = GetComponent<NavMeshAgent>();   // 获取对象的 NavMeshAgent 组件
 }

 public void MoveTo(Vector3 position)
 {
         agent.SetDestination(position);  //设置目的地,AI自动往目的地寻路,自动避障
         //agent.Resume();
 }

 public void Stop()
 {
        // agent.Stop();
 }

 void Update()
 {
         obj = GameObject.Find("Cube");
         MoveTo(obj.transform.position);  //朝cube的位置寻路
 }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容