- Home /
Question by
itamarperets30 · Jan 16, 2021 at 06:15 PM ·
line renderervisual-effectsnoisevector3.lerplightning
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: https://imgur.com/a/PUhfWn5
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());
}
screen-shot-2021-01-16-at-191628.png
(187.0 kB)
Comment
Your answer
Follow this Question
Related Questions
black line renderer 1 Answer
Changing size of a 2d object 1 Answer
How do i scale a prefab laser to hit.distance 1 Answer
LineRenderer.SetPositions Error 1 Answer
I can not see the line render 1 Answer