// 功能:
// 掛載對象:
//注意:
using UnityEngine;
using System.Collections;
public class scricubefollow : MonoBehaviour
{
//public Transform m_sph;
// 自身渲染器組件
LineRenderer m_Line;
// 自身導(dǎo)航組件
NavMeshAgent m_agent;
RaycastHit hit = new RaycastHit ();
void Awake ()
{
// 拿到兩個組件
m_Line = GetComponent ();
m_agent = GetComponent ();
//m_Line.enabled = false;
}
void Update ()
{
// 鼠標(biāo)鼠標(biāo)點擊哪里就去哪里
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit)) {
//讓主角前往目的地
m_agent.SetDestination (hit.point);
// 繪制線條 (在此處角色走完的路線不會消失)
DrawLine (hit);
}
}
// 繪制線條 (在此處角色走完的路線會消失)
//DrawLine (hit);
}
void DrawLine (RaycastHit hit)
{
// 繪制線條的核心代碼:
//1: 告訴我多少拐點
// m_Line.SetVertexCount ();
//2 告訴我拐點的位置在哪里
// m_Line.SetPositions ();
NavMeshPath path = new NavMeshPath ();
m_Line.SetPosition (path.corners.Length, hit.point);
// 計算從導(dǎo)航數(shù)據(jù)中計算出來所要走的路徑信息
m_agent.CalculatePath (hit.point, path);
//path.corners
// 設(shè)置要繪制的導(dǎo)航路線的拐點
m_Line.SetVertexCount (path.corners.Length);
// 設(shè)置拐點的位置
m_Line.SetPositions (path.corners);
// 也可使用循環(huán)來設(shè)置各個點
//for (int i = 0; i < path.corners.Length; i++) {
//m_Line.SetPosition (i, path.corners [i]);
//}
//激活線性組件
//m_Line.enabled = true;
}
}