Line Renderer Route

Open World - Scripts - Line Renderer Route

How to display a waypoint route with unity3D and C#

To display a waypoint route like a sat nav i use a the LineRenderer and give it an array of vector3's for the positions. Since i usethe LineRenderer on the inspector or via code there is a few changes to make the main setting to change is to change the Alignment to Transform Z and make sure use world space is true/checked set the transform euler angles to x 90 y 0 z 0.

line.useWorldSpace = true;
line.alignment = LineAlignment.TransformZ;
transform.eulerAngles = new Vector3(90, 0, 0);

LineRendererRoute.cs 1.1KB


using System.Collections;
using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class LineRendererRoute : MonoBehaviour
{
    public LineRenderer line;
    void Start()
    {
        line = GetComponent<LineRenderer>();
        line.useWorldSpace = true;
        line.alignment = LineAlignment.TransformZ;
        transform.eulerAngles = new Vector3(90, 0, 0);
    }
    public void SetPositions(Vector3[] points, bool animate)
    {
        if (animate)
        {
            StartCoroutine(LineAnimator(points));
        }
        else
        {
            LinePos(points);
        }
    }

    IEnumerator LineAnimator(Vector3[] points)
    {
        line.positionCount = points.Length;
        for (int i = 0; i < points.Length; i++)
        {
            line.SetPosition(i, points[i]);
            if (i % 2 == 0)
            {
                yield return null;
            }
        }
    }
    void LinePos(Vector3[] points)
    {
        line.positionCount = points.Length;
        line.SetPositions(points);        
    }
}
Powered by w3.css