2018-12-21 NavMeshAgent 预计算路径

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

public class TestAng : MonoBehaviour {

    public GameObject target;  //获取目标点,注意在面板中赋值
    NavMeshAgent mr;   //声明变量
                       // Use this for initialization
    void Start()
    {
        //获取到自身的NavMeshAgent组件
        mr = GetComponent();
        
    }
    
    // Update is called once per frame
    void Update()
    {
        //使用属性将目标点的坐标进行传递
       // mr.destination = target.transform.position;
        //使用方法获取目标点坐标,,和前一行代码作用相同
        //mr.SetDestination(target.transform.position);

        if (Input.GetKeyUp(KeyCode.A))
        {
            NavMeshPath path = new NavMeshPath();
           mr.CalculatePath(target.transform.position, path);
            float dis = 0;
            if (path.status==NavMeshPathStatus.PathComplete)
            {
                for (int i = 0; i < path.corners.Length; i++)
                {
                    if (i == path.corners.Length - 1)
                    {
                        continue;
                    }
                    dis += Vector3.Distance(path.corners[i], path.corners[i + 1]);
                }
                Debug.Log("自己计算路径点:" + dis);

                Debug.Log("Api计算路径点:" + path.GetCornersNonAlloc(path.corners));
            }
            
        }
    }

}

你可能感兴趣的:(2018-12-21 NavMeshAgent 预计算路径)