unity自动寻路相关注意事项

首先选择角色所在的地形,点击window->Navigation打开Navigation窗口,在Navigation下的object选项卡中选“Navigation  Static”其他保持默认即可,然后点击右下角“Bake”就可以了;


如果有障碍物,且障碍物不属于地形物体,需要对障碍物进行烘焙,方法是选择障碍物,在Navigation下的Object选项卡中勾选“Navigation Static”,“Navigation Layer”选择“Not Walkable”,打开Bake选项卡,根据需要修改相关参数,然后点击右下角“Bake”烘焙即可。

对于自动寻路的角色,需要添加“NavMeshAgent”组件,方法是点击菜单栏中“component->Naviga->NavMeshAgent”这样自动寻路的相关设置就完成了;

还需要为角色添加代码是他能够自动寻路:

public PlayerControl:MonoBehavior

{

    private NavMeshAgent agent;

    public float speed=6;

    void Start()

    {

        agent=GetComponent<NavMeshAgent>();//获取NavMeshAgent组件

     }

    void Update()

    {

        if(Input.GetMouseButtonDown(0))

        {

            Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);

            RayCastHit hitInfo;

            if(Physics.RayCast(ray,out hitInfo))

            {

                if(!hitInfo.Collider.name.Equals("Terrain"))

                    return;

                 else

                   {

                        Vector3 point=hitInfo.point;

                        transform.LookAt(new                                                                 Vector3(point.x,transform.position.y,point.z));

                        agent.speed=speed;

                        agent.SetDestination(point);

                    }

             }

        }

    }

}

你可能感兴趣的:(自动寻路)