【Unity3D】自动寻路(Nav Mesh Agent组件)

1.首先添加场景模型

2.为场景模型(寻路路径)添加NavMesh渲染,操作:Windows->Navigation->勾选Navigation Static选项->不勾选Generate选项->Navigation Area选为Walk able->Back栏调整Agent Radius参数->Bake按钮完成寻路渲染

3.为要移动物体添加Nav Mesh Agent组件

4.为要移动物体添加脚本

using UnityEngine;

using UnityEngine.UI;

using System.Collections;



public class bassTest : MonoBehaviour {

    //移动控制

    private NavMeshAgent Car;

    //货物模型,直接拖拉

    public GameObject Goods;

    //目标点(若要程序调用,请设为Static)

    public Vector3 target;

    void Awake () {

        Car = gameObject.GetComponent<NavMeshAgent> ();



        target = transform.position;



    }

    void Update () {           

        Car.SetDestination (target);

    }

}

 

你可能感兴趣的:(unity3d)