Unity AI导航寻路系统

第一步搭建场景

两个平面,一个立方体,一个胶囊,一座桥(立方体)

让这个胶囊自主找立方体

将除胶囊外其他场景中物体设为静态 

打开AI导航

烘培一个导航网格

给胶囊添加智能体

编辑AI寻路脚本

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

public class AINavigation : MonoBehaviour
{
    public GameObject target;
    private NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.destination = target.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

脚本绑定胶囊上

指定cube

效果

现在要实现另一个功能,就是鼠标点击哪儿,物体就往哪儿走。

这个功能在游戏中很常用。

发射一个射线,射线和地板相交的点,就是要走到的位置。

代码

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

public class AINavigation : MonoBehaviour
{
    public GameObject target;
    private NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.destination = target.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
        {
            RaycastHit hit;
            if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit))
            {
                agent.destination = hit.point;
            }
        }
    }
}

效果

鼠标点击哪就走到哪儿,有内味儿了

而且还可以自动避障哦

按下图修改场景

桥*2、胶囊*2

新增的桥设置静态

场景已改变,自主导航网格需要重新烘培


效果

默认走最短路径

面板解释

跳跃不超过0.4m,坡度不超过45°

自动导航的相关限制参数

现在让一个胶囊指定走一个桥,不能走另一个

选择胶囊,设置智能寻路属性

能走哪些区域,不能走哪些区域,设置好

指定两个桥的导航所属区域,一个front,一个back

一定要记住,修改参数后要重新烘焙

区域图

效果

搭建场景然后烘培

正常情况下,这两个胶囊会在墙下面停下来,够不着立方体

现在要实现让两个胶囊走天路,去够它

把导航网格不连接的区域把它link起来

给那个cube添加off mesh link组件

再烘培一下

已连接

但是现在还是原样,不能够着

要设置烘培高度

烘培后区域变化

效果

这个cube只是作为一个连接物,并不从上面过(逻辑有点奇怪)

导航障碍组件

修改场景

为桥添加导航障碍组件

编写脚本

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

public class NMOCtrl : MonoBehaviour
{
    private NavMeshObstacle obs;
    // Start is called before the first frame update
    void Start()
    {
        obs = this.GetComponent<NavMeshObstacle>();
        obs.enabled = true;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            if (obs)
            {
                obs.enabled = false;
                this.GetComponent<Renderer>().material.color = Color.green;
            }
        }
        if (Input.GetButtonUp("Fire1"))
        {
            if (obs)
            {
                obs.enabled = true;
                this.GetComponent<Renderer>().material.color = Color.red;
            }
        }
    }
}

绑定到桥上即可

修改参数

高度要大于1,不然没有阻碍效果!

效果

固定路线巡航

创建一个空对象

创建n个立方体做路径点

创建脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AIPath : MonoBehaviour
{
    public NavMeshAgent nma;
    public Transform[] pathpoints;
    int currentpointindex;
    // Start is called before the first frame update
    void Start()
    {
        nma.SetDestination(pathpoints[0].position);
    }

    // Update is called once per frame
    void Update()
    {
        if(nma.remainingDistance<nma.stoppingDistance)
        {
            currentpointindex = (currentpointindex + 1) % pathpoints.Length;
            nma.SetDestination(pathpoints[currentpointindex].position);
        }
    }
}

AI(胶囊)

胶囊智能体指定N.M.A.

把路径点拖入下面的数组

往这个字样上拖

场景静态化并烘培

运行

效果

无限循环

有限路径代码(停在最后一个路径点)

if(nma.remainingDistance<nma.stoppingDistance && currentpointindex<(pathpoints.Length-1))

效果

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值