Unity篇10:NavMeshAgent基本使用 - kudan-game/ArtArtist-Repo GitHub Wiki

本篇介绍Navigation及NavMeshAgent的基本使用


一、制作可走路的地形

1.将Plane及所有地面上的障碍物体在Inspector面板置为Navigation Static状态。(最好将所有静态障碍物归于一个gameobject下,挂到plane下也可)

2."Window -> AI -> Navigation" 面板,选中Bake,点击Bake,烘焙导航场景,可以看到Plane上加了一片蓝颜色区域,这就是可移动区域。

二、添加NavMeshAgent组件至玩家GameObject

几个常用的属性:

Radius[半径]:控制代理器这个“圆柱体”的半径;
Height[高度]:控制代理器这个“圆柱体”的高度;
Base Offset[基础偏移]:控制代理器这个“圆柱体”在垂直方向的偏移量;
Speed[速度]:导航的移动速度;
Angular Speed[转弯速度]:模型是Y 轴朝向目标点;如果不是,则转向;
Acceleration[加速度]:保持默认即可;
Stopping Distance[停止距离]:距离目的地多远的时候停止;
Auto Braking[自动停止]:保持默认勾选状态即可;

三、编写玩家移动脚本

基本代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;//引入AI,才能操作导航系统的组件
[RequireComponent(typeof(NavMeshAgent))]//表示此实体中需要有NavMeshAgent组件
public class PlayerMovementBangziAI : MonoBehaviour
{
    private NavMeshAgent m_NavMeshAgent;//人物身上的NavMeshAgent
    public Transform m_Transform;       //选择一个我们想要的目标Transform,这个Transform在游戏中由玩家控制position
    // Use this for initialization
    void Start()
    {
        m_NavMeshAgent = gameObject.GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        //方法:SetDestination(目标点位置);
        //m_NavMeshAgent.SetDestination(m_Transform.position);

        //属性:destination = 目标点位置;
        m_NavMeshAgent.destination = m_Transform.position;

        //以上代码作用一样,都是引导目的地的位置
    }
}

将m_Transform通过Unity实例化一个gameobject,可以在次gameobject上制作鼠标OnMouseClick()等事件脚本来获取m_Transform的position,进而控制人物的移动,等待。

注意玩家一定要落在plane上,否侧Agent组件无法生效。

附:NavMeshAgent类的属性和方法

NavMeshAgent.acceleration 加速度
NavMeshAgent.ActivateCurrentOffMeshLink 激活当前分离网格链接
NavMeshAgent.angularSpeed 角速度
NavMeshAgent.areaMask 区域遮挡
NavMeshAgent.autoBraking 自动制动
NavMeshAgent.autoRepath 自动重新获取路径
NavMeshAgent.autoTraverseOffMeshLink 自动穿过OffMeshLink
NavMeshAgent.avoidancePriority 逃避优先级
NavMeshAgent.baseOffset 基础偏移
NavMeshAgent.CalculatePath 计算路径
NavMeshAgent.CompleteOffMeshLink 完成分离网格链接
NavMeshAgent.currentOffMeshLinkData 当前关闭网格连接数据
NavMeshAgent.desiredVelocity 需求速度
NavMeshAgent.destination 目的地
NavMeshAgent.FindClosestEdge 寻找最近边缘
NavMeshAgent.GetAreaCost 获取区域成本
NavMeshAgent.hasPath 有路径
NavMeshAgent.height 高度
NavMeshAgent.isOnNavMesh 是否在导航网格上
NavMeshAgent.isOnOffMeshLink 是否在OffMeshLink上
NavMeshAgent.isPathStale 是否是旧路径
NavMeshAgent.Move 移动
NavMeshAgent.nextOffMeshLinkData 下一个OffMeshLink数据
NavMeshAgent.nextPosition 下个位置
NavMeshAgent.obstacleAvoidanceType 障碍逃避类型
NavMeshAgent.path 路径
NavMeshAgent.pathPending 路径等待
NavMeshAgent.pathStatus 路径状况
NavMeshAgent.radius 半径
NavMeshAgent.Raycast 射线投影
NavMeshAgent.remainingDistance 剩余距离
NavMeshAgent.ResetPath 重新设置路径
NavMeshAgent.Resume 恢复
NavMeshAgent.SamplePathPosition 样本路径位置
NavMeshAgent.SetAreaCost 设置区域成本
NavMeshAgent.SetDestination 设置目的地
NavMeshAgent.SetPath 设置路径
NavMeshAgent.speed 速度
NavMeshAgent.steeringTarget 转向目标
NavMeshAgent.Stop 刹车
NavMeshAgent.stoppingDistance 刹车距离
NavMeshAgent.updatePosition 更新位置
NavMeshAgent.updateRotation 更新旋转
NavMeshAgent.velocity 速度
NavMeshAgent.Warp 弯曲