# [Solved]How randomize linerender positions between start and end

I need to create a thunder effect in my game object using line renderer.
Actually, the progress is near of the final effect but when I randomize the vertex positions I really make vertices too random and I need to create an effect considering the start point and final point.

``````    private void RecalcularZigZag()
{
float rMin = -0.2f;
float rMax = 0.2f;
Vector3 pos = torreAtirou.GetComponent<Torre>().raio.empty.transform.position;
for (int p = 2; p < VERTICES - 2; p++)
{
pos += new Vector3(Random.Range(rMin, rMax), Random.Range(rMin, rMax), Random.Range(rMin, rMax));
GetComponent<LineRenderer>().SetPosition(p, pos);
}
GetComponent<LineRenderer>().SetPosition(VERTICES - 1, alvo.transform.position);
}
``````

I did the setup of my line renderer with 7 positions and need to do something like this:

But the final result is a incorrect vertext position considering the start and final point:

Some ideas that I need to do to create valid random sequences of vertext considering the projection of the line renderer (from a point to another) ?

I think the most sensible way to do this using a line renderer would be:

Determine start / end points as the first and last elements of a Vector array.
Use Vector.Lerp in a loop to fill the array with N equidistant points between start/end.
Add randomness to these N points.
Feed the points array into the line renderer position array.

You can even run this code multiple times per “bolt” to create an animated zig-zag.

``````Vector3[] points = new Vector3[10];
points[0] = start;
points[9] = end;
for (int i = 1; i < 9; i++) {
points *= Vector3.Lerp(start, end, (float)i/10f);*
``````

points += Random.OnUnitSphere * zaggyness;
}
You could get fancy and make the zaggyness be informed by the [cross-product][1] of start and end, such that any deviation from “a straight line” is perpendicular to the start/end line. That would create a more consistent “zig-zag” shape.
_*[1]: http://docs.unity3d.com/ScriptReference/Vector3.Cross.html*_

Thanks for the reply, I did some tests and wrote this to resolve my problem:

``````    private void RecalcularZigZag()
{
Vector3 origem = torreAtirou.GetComponent<Torre>().raio.empty.transform.position;
Vector3 destino = alvo.transform.position;
Vector3[] posicoes = new Vector3[VERTICES];

float rMin = -0.5f;
float rMax = 0.5f;

posicoes[0] = origem;
for (int p = 1; p < VERTICES; p++)
{
Vector3 pos = Vector3.Lerp(origem, destino, p / 4f);
pos.x += Random.Range(rMin,rMax);
pos.y += Random.Range(rMin, rMax);
posicoes[p] = pos;
}
posicoes[VERTICES - 1] = destino;

for (int p = 0; p < VERTICES; p++)
{
GetComponent<LineRenderer>().SetPosition(p, posicoes[p]);
}
}
``````