Lightning Using A Line Renderer

So far I have created a line renderer that follows a bezier curve:


I am now trying to make the line look like lightning. I have tried Perlin noise and it didn’t really work as I expected:

Each point sort of jerks around wildly and it looks odd. I want the effect to scroll like a texture if that makes any sense.

Here is my code:

[SerializeField] Transform point1;
[SerializeField] Transform point2;
[SerializeField] Transform point3;

[Min(1)] [SerializeField] int vertexResolution = 12;
[SerializeField] Vector2 noiseScale;

LineRenderer lineRenderer;

void Start() => lineRenderer = GetComponent<LineRenderer>();

void Update()
{
    List<Vector3> pointPositions = new List<Vector3>();
    for (float i = 0; i <= 1; i += 1f / vertexResolution)
    {   
        Vector2 tangentLineA = Vector2.Lerp(point1.position, point2.position, i);
        Vector2 tangentLineB = Vector2.Lerp(point2.position, point3.position, i);
        // Each point along the curve
        Vector2 bezierPoint = Vector2.Lerp(tangentLineA, tangentLineB, i);

        // Checks if the current vertex is the first or the last, which makes both ends of the lightning stay still.
        if (i >= 1f / vertexResolution && i <= 1 - 1f / vertexResolution)
        {
            // Creates noise and maps it from 0 - 1 to -1 and 1.
            float noise = Mathf.PerlinNoise(Time.time, 0) * 2 - 1;

            // Changes the point's position randomly based on the noise.
            Vector2 point = new Vector2(bezierPoint.x + noise * noiseScale.x, bezierPoint.y + noise * noiseScale.y);
            pointPositions.Add(point);
        }
        else
        {
            pointPositions.Add(bezierPoint);
        }
    }t

    lineRenderer.positionCount = pointPositions.Count;
    lineRenderer.SetPositions(pointPositions.ToArray());
}

I’ve been looking all over to find this post again. It’s been a while, but I think I slightly changed the code.
It was a major help to me. Here’s the code and the script ingame.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jolt : MonoBehaviour
{
    protected virtual IEnumerator RandomizePath(Unit unit, Vector3[] points, Transform rendererParent, Transform targetParent, LineRenderer renderer)
    {
        while (renderer != null && unit != null)
        {
            List<Vector3> pointPositions = new List<Vector3>();

            for (int j = 0; j < m_Resolution; j++)
            {
                if (rendererParent != null && rendererParent.gameObject.activeInHierarchy)
                {
                    points[0] = rendererParent.position;
                }
                if (targetParent != null && targetParent.gameObject.activeInHierarchy)
                {
                    points[2] = targetParent.position;
                }

                points[1] = points[0] + (unit.ShootPoint.up + unit.ShootPoint.right) * m_CurveIntensity;

                Vector3 tangentLineA = Vector3.Lerp(points[0], points[1], j * (1f / (m_Resolution - 1)));
                Vector3 tangentLineB = Vector3.Lerp(points[0], points[2], j * (1f / (m_Resolution - 1)));
                Vector3 bezierPoint = Vector3.Lerp(tangentLineA, tangentLineB, j * (1f / (m_Resolution - 1)));
                Vector2 randomVector = Random.insideUnitCircle;

                if (j > 0 && j < m_Resolution - 1)
                {
                    bezierPoint = new Vector3(
                        bezierPoint.x + randomVector.x * m_JitterIntensity,
                        bezierPoint.y + randomVector.y * m_JitterIntensity,
                        bezierPoint.z);
                }

                pointPositions.Add(bezierPoint);
            }

            renderer.positionCount = pointPositions.Count;
            renderer.SetPositions(pointPositions.ToArray());

            yield return new WaitForSeconds(m_JitterFrequency);
        }
    }
}