需求:点击地面的时候,在角色的移动之前,显示出角色的未来行走路径。
1.主要原理
http://docs.unity3d.com/ScriptReference/NavMeshPath-corners.html
2.主要效果
3.关键代码
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); } } }