Unity3D实用组件:NavMesh Agent

一、简介

NavMeshAgent components help you to create characters which avoid each other while moving towards their goal. Agents reason about the game world using the NavMesh
and they know how to avoid each other as well as other moving obstacles. Pathfinding and spatial reasoning are handled using the scripting API of the NavMesh Agent.
NavMesh Agent是一个非常好用的角色移动控制组件。它可以通过NavMesh来标记可到达和不可到达的区域。同时它自带寻路和空间推理的脚本,可以控制角色朝着目标移动却不和其他Agent彼此影响,同时它也知道如何避开对方及其他障碍物。

二、NavMesh设置

需要作为障碍物使得Agent无法通过的物体区域需要将其设置为静态(Navigation Static)。这样在后续烘焙过程中才能产生效果。
在这里插入图片描述

三、Inspector面板参数调节

Alt
在Inspector面板中可以对Agent对象的参数进行调整。以下是一些常用的参数及其使用说明。

参数说明
Speed运动速度
Angular Speed旋转速度
Acceleration加速度
Stopping Distance停止距离(距目标多远时停止运动)
Quality品质(高品质的避障会减少Agent的重叠概率,但会有更大计算消耗)
Priority优先级(在执行回避时,优先级较低的代理将被该代理忽略)

点击Agent Type -> Open Agent Settings后开启以下窗口,在Bake窗口中可以调节Agent检测的高度、半径大小以及可以爬坡的坡度等参数,用以确定Agent的可移动区域。Alt
在调整完后点击Bake对场景进行再次烘焙。

四、示例代码

1.为角色添加代理

//PlayerController.cs
private NavMeshAgent agent; 

2.在Awake中获取组件

//PlayerController.cs
private void Awake()
{
    agent = GetComponent<NavMeshAgent>();
}

3.移动到目标位置

//PlayerController.cs
	//将agent的移动目标设置为传入的target的位置坐标
    public void MoveToTarget(Vector3 target)
    {
        agent.isStopped = false;
        agent.destination = target;
    }

其中target可以由鼠标点击的位置来确定,代码实现如下所示:

//MouseManager.cs
    /// <summary>
    /// 点击鼠标控制角色移动
    /// </summary>
    public void MouseController()
    {
        if (Input.GetMouseButtonDown(0) && hitInfo.collider!=null)
        {
            switch(hitInfo.collider.gameObject.tag)
            {
                case "Ground":
                    EventHandler.CallMouseClickedEvent(hitInfo.point);
                    break;
            }
        }
    }

在这里需要为移动区域添加Ground标签

在代码中使用了事务代理,注册MouseClickedEvent并在PlayerController.cs中进行实现。

//EventHandler.cs
    /// <summary>
    /// 鼠标点击相应事件
    /// </summary>
    public static event Action<Vector3> MouseClickedEvent;

    public static void CallMouseClickedEvent(Vector3 target)
    {
        MouseClickedEvent?.Invoke(target);
    }

注册事件

//PlayerController.cs
    private void OnEnable()
    {
        EventHandler.MouseClickedEvent += OnMouseClickedEvent;
    }

    private void OnDisable()
    {
        EventHandler.MouseClickedEvent -= OnMouseClickedEvent;
    }
//PlayerController.cs
    private void OnMouseClickedEvent(Vector3 target)
    {
        MoveToTarget(target);
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NavMeshAgentUnity引擎中的一个组件,用于控制角色的移动和寻路。通过使用NavMeshAgent,开发者可以为角色创建一个导航网格,标记可到达和不可到达的区域,并使角色能够自动寻找最优路径,避免与其他Agent和障碍物发生碰撞。NavMeshAgent组件自带了寻路和空间推理的脚本,可以帮助角色朝着设定的目标移动,并避免与其他Agent相互干扰。 要为角色添加NavMeshAgent组件,可以通过在角色的脚本中添加以下代码来实现:private NavMeshAgent agent;在Unity中,还有一个Unity NavMesh 2D寻路的功能可用于2D游戏的导航和寻路。你可以通过下载一个相关的存储库或将其添加到项目的Package Manager清单中来使用这个功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity3D实用组件NavMesh Agent](https://blog.csdn.net/float_freedom/article/details/126179067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [NavMeshPlus:Unity NavMesh 2D寻路](https://download.csdn.net/download/weixin_42129797/15108830)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值