基于unity的路线引导

需求:点击地面的时候,在角色的移动之前,显示出角色的未来行走路径。
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); } } }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容