基于unity的路線引導(dǎo)

需求:點擊地面的時候,在角色的移動之前,顯示出角色的未來行走路徑。
1.主要原理
http://docs.unity3d.com/ScriptReference/NavMeshPath-corners.html
2.主要效果

路徑效果

3.關(guān)鍵代碼
private IEnumerator ShowTipPoint() { yield return new WaitForSeconds(0.05f); Vector3 originalPathPoint = transform.position; Vector3 endPathPoint = navMeshAgent.path.corners[navMeshAgent.path.corners.Length - 1]; Vector3 forward = endPathPoint - originalPathPoint; GameObject tipGameObject; if (navMeshAgent.path.corners.Length < 2) { yield break; } for (int i = 0; i < pathList.Count; i++) { Destroy(pathList[i]); } pathList.Clear(); originalPathPoint = transform.position; endPathPoint = navMeshAgent.path.corners[navMeshAgent.path.corners.Length - 1]; GameObject tipPath = Instantiate(TipCicle, endPathPoint, Quaternion.identity) as GameObject; tipPath.transform.Rotate(new Vector3(-90, 0, 0)); for (int i = 1; i < navMeshAgent.path.corners.Length; i++) { Vector3 lastPoint = navMeshAgent.path.corners[i - 1]; if (i > 1) { originalPathPoint = lastPoint; } tipGameObject = Instantiate(TipPath, lastPoint, Quaternion.identity) as GameObject; tipGameObject.transform.Rotate(new Vector3(-90, 0, 0)); tipGameObject.transform.Translate(new Vector3(0f, 0.05f, 0f),Space.World); pathList.Add(tipGameObject); tipGameObject.GetComponent<SpriteRenderer>().DOFade(0, 1f); Vector3 currentPoint = navMeshAgent.path.corners[i]; forward = Vector3.Normalize(currentPoint - lastPoint); for (int j = 0; j < Vector3.Distance(currentPoint, lastPoint) / 0.3f; j++) { yield return new WaitForSeconds(0.03f); tipGameObject = Instantiate(TipPath, j * 0.3f * forward + originalPathPoint, Quaternion.identity) as GameObject; tipGameObject.transform.Rotate(new Vector3(-90, 0, 0)); tipGameObject.transform.Translate(new Vector3(0f, 0.05f, 0f), Space.World); pathList.Add(tipGameObject); tipGameObject.GetComponent<SpriteRenderer>().DOFade(0, 1f); } } }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容