unity 3D AI自动寻路

unity 3D AI自动寻路

    • 建造一个简单的场景
    • 选中两个球之外所有的物体,然后在Inspector——Static——Navigation Static
    • 选中要寻路的物体添加在导航栏选中Component——Navigation——Nav Mesh Agent组件
    • 在导航栏打开Window——Navigation窗口
    • 在Navigation窗口里面选择Bake然后再选择右下角的Bake
    • 上面所有的设置好后上代码,把这个代码组件放到寻路的物体上
    • 然后把目标拖到代码组件的Target里面
    • 运行就OK了

NavMesh(导航网格)是3D游戏世界中用于实现动态物体自动寻路的一种技术,将游戏中复杂的结构组织关系简化为带有一定信息的网格,在这些网格的基础上通过一系列的计算来实现自动寻路。。导航时,只需要给导航物体挂载导航组建,导航物体便会自行根据目标点来寻找最直接的路线,并沿着该线路到达目标点。

建造一个简单的场景

unity 3D AI自动寻路_第1张图片

选中两个球之外所有的物体,然后在Inspector——Static——Navigation Static

unity 3D AI自动寻路_第2张图片

选中要寻路的物体添加在导航栏选中Component——Navigation——Nav Mesh Agent组件

unity 3D AI自动寻路_第3张图片

在导航栏打开Window——Navigation窗口

unity 3D AI自动寻路_第4张图片

在Navigation窗口里面选择Bake然后再选择右下角的Bake

unity 3D AI自动寻路_第5张图片

上面所有的设置好后上代码,把这个代码组件放到寻路的物体上

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

public class AI : MonoBehaviour {

    public GameObject target;
    private NavMeshAgent navMeshAgent;

	void Start ()
    {
        navMeshAgent = GetComponent();
        if(navMeshAgent==null)
        {
            navMeshAgent = gameObject.AddComponent();
        }
	}
	

	void Update ()
    {
            navMeshAgent.SetDestination(target.transform.position);
	}
}

然后把目标拖到代码组件的Target里面

unity 3D AI自动寻路_第6张图片

运行就OK了

你可能感兴趣的:(unity)