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());
}