Unity 实用教程之 Nav导航箭头路线绘制生成

2025-10-21 11:34:01

1、打开Unity,新建一个空工程,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

2、在场景中,简单的布置一些环境物体,并设置为 Static,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

Unity 实用教程之 Nav导航箭头路线绘制生成

3、在顶部菜单栏 Window - Navgation, Bake 环境,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

Unity 实用教程之 Nav导航箭头路线绘制生成

4、在工程中,新建一个脚本 ArrowFindPath,右键 Open C# Project 打开脚本,进行编辑,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

5、ArrowFindPath 脚本的具体代码和代码解释如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

Unity 实用教程之 Nav导航箭头路线绘制生成

Unity 实用教程之 Nav导航箭头路线绘制生成

6、ArrowFindPath 脚本具体内容如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class ArrowFindPath : MonoBehaviour {

    private NavMeshAgent _navPlayer;

    private NavMeshPath _navPath;

    public float tileSpacing = 0.5f;

    public LineRenderer lineGameObject;

    public GameObject directionPrefab;

    private List<GameObject> arrowList = new List<GameObject>();

    // Use this for initialization

    void Start () {

        _navPlayer = transform.GetComponent<NavMeshAgent>();

        _navPath = new NavMeshPath();

    }

    // Update is called once per frame

    void Update () {

                if(Input.GetMouseButtonDown(0)) {

                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                    RaycastHit hit;

                    if(Physics.Raycast(ray,out hit,Mathf.Infinity)) {

                                print ("hit :" + hit.point);

                                _navPlayer.SetDestination(hit.point);

                        NavMesh.CalculatePath(transform.position, hit.point, NavMesh.AllAreas, _navPath);

                                DrawPath(_navPath);

                    }

                }

    }

    /// <summary>

    /// Draws the path.

    /// </summary>

    /// <param name="navPath">Nav path.</param>

    void DrawPath(NavMeshPath navPath)

    {

        List<GameObject> arrows = arrowList;

        StartCoroutine(ClearArrows(arrows));

        arrowList.Clear();

        //If the path has 1 or no corners, there is no need to draw the line

        if (navPath.corners.Length < 2)

        {

            print ("navPath.corners.Length < 2");

            return;

        }

        // Set the array of positions to the amount of corners...

        lineGameObject.positionCount = navPath.corners.Length;

        Quaternion planerot = Quaternion.identity;

        for (int i = 0; i < navPath.corners.Length; i++)

        {

            // Go through each corner and set that to the line renderer's position...

            lineGameObject.SetPosition(i, navPath.corners[i]);

            float distance = 0;

            Vector3 offsetVector = Vector3.zero;

            if (i < navPath.corners.Length - 1)

            {

                //plane rotation calculation

                offsetVector = navPath.corners[i + 1] - navPath.corners[i];

                planerot = Quaternion.LookRotation(offsetVector);

                distance = Vector3.Distance(navPath.corners[i + 1], navPath.corners[i]);

                if (distance < tileSpacing)

                    continue;

                planerot = Quaternion.Euler(90, planerot.eulerAngles.y, planerot.eulerAngles.z);

                //plane position calculation

                float newSpacing = 0;

                for (int j = 0; j < distance / tileSpacing; j++)

                {

                    newSpacing += tileSpacing;

                    var normalizedVector = offsetVector.normalized;

                    var position = navPath.corners[i] + newSpacing * normalizedVector;

                    GameObject go = Instantiate(directionPrefab, position+Vector3.up, planerot);

                    arrowList.Add(go);

                }

            }

            else

            {

                GameObject go = Instantiate(directionPrefab, navPath.corners[i]+Vector3.up, planerot);

                arrowList.Add(go);

            }

        }

    }

    /// <summary>

    /// Clears the arrows.

    /// </summary>

    /// <returns>The arrows.</returns>

    /// <param name="arrows">Arrows.</param>

    private IEnumerator ClearArrows(List<GameObject> arrows)

    {

        if (arrowList.Count == 0)

            yield break;

        foreach (var arrow in arrows)

            Destroy(arrow);

    }

}

7、在场景中添加一个 Capsule,并在上面添加一个 NavMeshAgent,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

8、在场景中,新建一个 GameObject,命名为 LineRenderer,并在上面添加一个LineRenderer组件,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

9、导入一个箭头的 图片,并修改成精灵图,拖到场景中,调整好合适的精灵图比例,在拖到工程中,作为预制体,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

Unity 实用教程之 Nav导航箭头路线绘制生成

10、选中场景中的 Capsule,添加脚本 ArrowFindPath,赋值 LineRenderer 和 箭头预制体,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

11、运行场景,在场景中随便点击们就会生成,箭头路线,具体如下图

Unity 实用教程之 Nav导航箭头路线绘制生成

12、到此,《Unity 实用教程之 Nav导航箭头路线绘制生成》讲解结束,谢谢

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢