[Unity][NavMeshAgent]怎麼計算角色走過的路程長度

 

1.確定路程長度,得獲得 2個座標點,起點 和 終點。

float f = Vector3.Distance(a,b);

2.對累計走過得長度進行累加

float distance += f;

 

這個條件得在什麼情況下,計算累計行走得長度?

當角色開始移動,移動得速度從0-1得時候,到達目的地 才進行累加。

player_go.transform.GetComponent<NavMeshAgent>().path.corners

是NavMeshAgent組件累計行走得時候 把所有得路徑得點得座標放入其中。

 

獲得NavMeshAgent導航座標Vector3數組

player_go.transform.GetComponent<NavMeshAgent>().path.corners;

 

這三者的值 都是 相同的。

player_go.transform.position;//玩家控制的角色的世界座標點
player_go.transform.GetComponent<NavMeshAgent>().nextPosition;//
player_go.transform.GetComponent<NavMeshAgent>().path.corners[0];

 

 

 

...
using UnityEngine.AI;
class...
{
...
    private NavMeshAgent agent;
    private float walkDistance =0;//總共行走得距離
    private float distance = 0;//兩點之間 暫時得距離
    public Vector3[] vector3s;//player_go.transform.GetComponent<NavMeshAgent>().path.corners用於觀察這個數組是如何運行得

    int cornersLength = 0;//長度
    private int isReachDestination = 0;//
    private float speedPercent = 0;//角色 得移動速度
    private  Vector3 nextPos;


    public List<Vector3> list_distance = new List<Vector3>();//using System.Collections.Generic;
...
    start()
    {
    ...
        agent = GetComponent<NavMeshAgent>();
        walkDistance = 0;
    ...
    }//start()
    ...

    update()
    {
        ...
        vector3s = agent.path.corners;
        cornersLength = vector3s.Length;

        if (player_go != null
            && player_go.transform.GetComponent<NavMeshAgent>() != null)
        {
            agent = player_go.transform.GetComponent<NavMeshAgent>();
            speedPercent = agent.velocity.magnitude / agent.speed;//在Update函數裏面進行調用
        }//
        ...
        nextPos = agent.nextPosition;//角色當前的及時座標
        if (speedPercent == 0
            && Input.GetMouseButtonDown(0))//當角色的速度爲0,鼠標點擊左鍵的時候
        {
            list_distance.Add(nextPos);//行走路徑數組 添加起始出發點 座標
        }
        if (cornersLength >= 2)//當NavMeshAgent導航路徑點數組,開始導航,角色開始移動
        {
            Debug.Log("======================>>>>>>>");
            distance = Vector3.Distance(agent.path.corners[1],//判斷角色的下一個 途中 路徑點的座標 
                   agent.path.corners[0]);//判斷 角色當前的座標
            //distance的差值 可以隨時調整,誤差值過大會影響效果
            Debug.Log("======================>>>>>>>"+ distance);
            if (distance < 1.8f)//角色移動已經到達 其中的路徑點的座標
                list_distance.Add(agent.path.corners[0]);//行走路徑數組 路徑點 座標

        }
    ...
    }//update()

...
}//class

通過update函數裏面vector3s = player_go.transform.GetComponent<NavMeshAgent>().path.corners;

來觀測 這個corners是如何工作的,當角色 使用NavMeshAgent 進行導航,目的地座標有多個 拐點的時候,vector3s[0]和vector3s[1]

player_go.transform.GetComponent<NavMeshAgent>().path.corners[0]

和player_go.transform.GetComponent<NavMeshAgent>().nextPosition永遠是當前角色的座標。transform.position

 

當鼠標點擊一次的時候,獲得 移動路徑點的數組

判斷nextpos是否與 已經獲得的 移動路徑點的數組 中的某個 路徑點座標 相同,如果相同 就 把上一個 路徑點的座標與nextpos的距離 進行判斷。

 

判斷當前角色的及時 世界座標與 路徑點數組 中的 下一個路徑點的 座標 是否 接近,

如果是,就把 下一個路徑點的 座標 放入 走過的 所有 座標數組List<Vector3>list_distance 中。

獲得 總共行走的 路程 遍歷 數組list_distance中的 座標點,並且用Vector3.Distance(pos1,pos0);進行計算即可。

 

...

        if (speedPercent == 0
            && Input.GetMouseButtonDown(0))//當角色的速度爲0,鼠標點擊左鍵的時候
        {
            list_distance.Add(nextPos);//行走路徑數組 添加起始出發點 座標
        }
        if (cornersLength >= 2)//當NavMeshAgent導航路徑點數組,開始導航,角色開始移動
        {
            //Debug.Log("======================>>>>>>>");
            distance = Vector3.Distance(agent.path.corners[1],//判斷角色的下一個 途中 路徑點的座標 
                   agent.path.corners[0]);//判斷 角色當前的座標
            //distance的差值 可以隨時調整,誤差值過大會影響效果
            //Debug.Log("======================>>>>>>>"+ distance);
            if (distance < 1.8f)//角色移動已經到達 其中的路徑點的座標
            {
                index++;
                list_distance.Add(agent.path.corners[1]);//行走路徑數組 路徑點 座標
                walkDistance = walkDistance + distance;
                Debug.Log("          ========== distance: " + distance+"///"+ walkDistance);
            }

        }
...

 

 

相關資料:

1.[Unity]NavMeshAgent自動尋路判斷到達目的地

2.

 

參考資料:

1.unity 兩點之間的距離

2.NavMeshPath.corners

3.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章