多语言展示
当前在线:120今日阅读:61今日分享:18

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

Unity 实用教程之 Nav导航箭头路线绘制。本节介绍,在Unity开发中,导航的时候实现导航箭头的绘制生成的方法(类似案例可参考如下经验链接),具体如下17Unity 实用教程之 简单地图导航循环长箭头实现
工具/原料

Unity

方法/步骤
1

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

2

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

3

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

4

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

5

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

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 arrowList = new List();    // Use this for initialization    void Start () {        _navPlayer = transform.GetComponent();        _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);                    }                }    }    /// 

    /// Draws the path.    ///     /// Nav path.    void DrawPath(NavMeshPath navPath)    {        List 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);            }        }    }    ///     /// Clears the arrows.    ///     /// The arrows.    /// Arrows.    private IEnumerator ClearArrows(List arrows)    {        if (arrowList.Count == 0)            yield break;        foreach (var arrow in arrows)            Destroy(arrow);    }}

8

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

9

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

10

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

11

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

12

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

注意事项

您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言

推荐信息