Drawing Connected Straight Lines on a Surface Using Line Renderer


LineCreator.cs
using System.Linq;
using UnityEngine;

public class LineCreator : MonoBehaviour
{
    LineRenderer lineRenderer;
    RaycastHit hit;

    void Awake()
    {
        lineRenderer = gameObject.AddComponent<LineRenderer>();
        lineRenderer.startWidth = 0.1f;
        lineRenderer.material = Resources.FindObjectsOfTypeAll<Material>().SingleOrDefault(m => m.name == "Line");
        lineRenderer.positionCount = 0;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                AddLinePoints(hit.point + hit.normal.normalized * 0.1f);
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            ClearLinePoints();
        }
    }

    void AddLinePoints(Vector3 point)
    {
        lineRenderer.positionCount++;
        lineRenderer.SetPosition(lineRenderer.positionCount-1, point);
    }

    void ClearLinePoints()
    {
        lineRenderer.positionCount = 0;
    }
}