unity 自動尋路顯示剩餘路徑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class test : MonoBehaviour
{
    NavMeshAgent m_Agent;
    RaycastHit m_HitInfo = new RaycastHit();
    public LineRenderer _lineRenderer;
    void Start()
    { m_Agent = GetComponent<NavMeshAgent>(); }

    void Update()
    {
        if (Mathf.Abs(m_Agent.remainingDistance) < 1.5f)
        {
            _lineRenderer.positionCount = 0;
            _lineRenderer.gameObject.SetActive(false);
        }


        if (_lineRenderer.gameObject.activeInHierarchy)
        {
            Vector3[] _path = m_Agent.path.corners;//儲存路徑  
            var path = _path;
            _lineRenderer.positionCount= _path.Length;//設置線段數  
            for (int i = 0; i < _path.Length; i++)
            {
                Debug.Log(i + "= " + _path[i]);
                _lineRenderer.SetPosition(i, _path[i]);//設置線段頂點座標 
            } 
                
          }

        if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray.origin, ray.direction, out m_HitInfo))
            {

                m_Agent.destination = m_HitInfo.point;
                //m_Agent.Stop();  
                _lineRenderer.gameObject.SetActive(true);
            }
        }  
           
              
         } 
        
}

 

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