Generate a mesh around a LineRenderer

Hello, I was wondering if there is an easy way to generate a mesh for a LineRenderer?

I followed this tutorial to make a spline and I was hoping I could attach unity’s new navmesh surface component to it.

Sadly I didn’t realize that the LineRenderer wasn’t a mesh and so it won’t work with the navigation system.

I have all the vertexes for the line. Is the only way to do it to generate mesh individually?

Here is some starter code. There are plenty of examples on how to dynamically create a mesh from points, so my answer here is going to focus on creating a group of points from a line. In this case, there will be a “left” side and a “right” side, and you can create a triangle strip using the two sets of points. The left side is one meter to the left of center, and the right side is one meter to the right of center, creating a path two meters wide.

103987-path.jpg

This code is not a solve-all-cases piece of code. The sharper the angles of the turn, the more the weaknesses of this approach will show. That said, it’s a fairly simple piece of code to understand, and you can build upon it to deal with the edge cases that you may encounter.

Also remember: You can run it in editor, and then prefab-out the generated result which you can then re-use without having to go through the process of generation.

This code finds & debug displays the left & right side of the line, but does not create the mesh.

using UnityEngine;

public class PathScript : MonoBehaviour {

    public LineRenderer line;
    
	// Use this for initialization
	void Start () {
        var caret = new GameObject();
        Vector3 left, right; // A position to the left of the current line

        // For all but the last point
		for (var i = 0; i < line.positionCount - 1; i++)
        {
            caret.transform.position = line.GetPosition(i);
            caret.transform.LookAt(line.GetPosition(i + 1));
            right = caret.transform.position + caret.transform.right;
            left = caret.transform.position - caret.transform.right;
            Debug.DrawLine(line.GetPosition(i), line.GetPosition(i) + Vector3.up, Color.white, 60f); // Just to visualize what's going on
            Debug.DrawLine(left, left + Vector3.up, Color.blue, 60f); // Just to visualize what's going on
            Debug.DrawLine(right, right + Vector3.up, Color.red, 60f); // Just to visualize what's going on
        }

        // Last point looks backwards and reverses
        caret.transform.position = line.GetPosition(line.positionCount - 1);
        caret.transform.LookAt(line.GetPosition(line.positionCount - 2));
        right = caret.transform.position - caret.transform.right;
        left = caret.transform.position + caret.transform.right;
        Debug.DrawLine(caret.transform.position, caret.transform.position + Vector3.up, Color.white, 60f); // Just to visualize what's going on
        Debug.DrawLine(left, left + Vector3.up, Color.blue, 60f); // Just to visualize what's going on
        Debug.DrawLine(right, right + Vector3.up, Color.red, 60f); // Just to visualize what's going on
    }
}

I’d suggest giving the LineRenderer component’s built-in BakeMesh method a try.