unity-Navigation寻路-基础


效果图
这里写图片描述



1. 建立地形,生成寻路网格

  1. 随便 Create PlaneCube选中这两个物体的状态下

  2. 打开寻路设置窗口 Window -> Navigation -> Object , 勾选 Navigation Static,

    这里写图片描述

  3. Navigation 窗口点击 Bake 生成寻路网格。寻路数据自动生成在 场景同名文件夹下的 NavMesh.asset

    这种静态的增加障碍物,动态增加在这里 http://blog.csdn.net/yangxuan0261/article/details/52821833

    这里写图片描述

    Bake 之后 物体的变为 Navigation Static 状态

    可行通区域就出来了, 边界值可以通过 Bake -> Agent Radius 中设置大小, 最小是 0.01, 越小bake的速度就越慢
    这里写图片描述

  4. 自定义地形

    地形可以 3d软件如 3dsmax 中构建一个自定义的 地形mesh

    然后导出 fbx 给 unity, bake 寻路数据

    效果图


2. 创建一个行走的 Actor(黑色眼镜那个) 和几个行走点(绿色球)


3. 给 Actor 添加行走

  1. Actor 身上挂个 Nav Mesh Agent

    这里写图片描述

  2. 添加个行走控制的脚本

    这里写图片描述

    NavMove.cs

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class NavMove : MonoBehaviour
    {
    
        public NavMeshAgent agent;
        public Transform[] destPos = new Transform[] { };
        int currentPoint = 0;
    
        // Use this for initialization
        void Start()
        {
            agent.Stop();
            StartCoroutine(Move());
        }
    
        IEnumerator Move()
        {
            //enable agent updates
            agent.Resume();
            agent.updateRotation = true;
    
            agent.SetDestination(destPos[currentPoint].position);
            yield return StartCoroutine(WaitForDestination());
    
            StartCoroutine(NextWaypoint());
        }
    
        IEnumerator WaitForDestination()
        {
            yield return new WaitForEndOfFrame();
            while (agent.pathPending)
                yield return null;
            yield return new WaitForEndOfFrame();
    
            float remain = agent.remainingDistance;
            while (remain == Mathf.Infinity || remain - agent.stoppingDistance > float.Epsilon
            || agent.pathStatus != NavMeshPathStatus.PathComplete)
            {
                remain = agent.remainingDistance;
                yield return null;
            }
    
            Debug.LogFormat("--- PathComplete to pos:{0}", currentPoint);
        }
    
        IEnumerator NextWaypoint()
        {
            currentPoint++;
            currentPoint = currentPoint % destPos.Length;
            Transform next = destPos[currentPoint];
            agent.SetDestination(next.position);
            yield return StartCoroutine(WaitForDestination());
    
            StartCoroutine(NextWaypoint());
        }
    }

    这种使用 Coroutine 移动到目的点 的方式是在 Simple Waypoint System 插件中的 navMove.cs 中看到的。利用 Coroutine 来达到 分帧执行 使得代码变得非常简洁。

done


高级设置

agent 指定 layer 行走区域

  1. 添加 指定layer: WalkArea

  2. bake 寻路数据时, 指定layer 为 WalkArea

  3. agent 中指定行走的区域为 WalkArea

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝶泳奈何桥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值