Hello everyone.
Ive been trying to create a procedural electric linerenderer that is dynamic between two transforms.
But to no avail.
Thats why im turning to you guys.
I really need help with this one.
this is the code to its bare minimum.
using UnityEngine;
using System.Collections;
public class Lightning : MonoBehaviour
{
private LineRenderer lineRenderer;
private int numOfPoints = 5;
public Transform lineStart;
public Transform lineEnd;
private void Start()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.positionCount = numOfPoints;
}
void Update ()
{
lineRenderer.SetPosition(0, lineStart.transform.position);
lineRenderer.SetPosition(1, Random.Range()); // need help here to keep this as a random point but within a relation between the start and end transform positions.
lineRenderer.SetPosition(2, lineEnd.transform.position);
}
}
how would i go about in making mutliple points between the start and end line transforms, but still have them moving according to a random range vector3?
You basically need to find the midpoint between the two objects, and then add some randomness onto that. You can use Lerp to find the midpoint, and then Random.insideUnitSphere for the randomness. So something like:
As for the next step of turning this into an arbitrary number of points, you will basically need to use a for loop, with the above line as the meat of the loop. You’ll replace the “1” with the loop control variable, and replace the “0.5f” with that number divided by the total number of points.
for (int i=0;i<numPoints;i++) {
float lerpValue = (float)i / numPoints; //if you don't typecast to float here, it'll do the math as integers and come up with 0 every time
lineRenderer.SetPosition(i, Vector3.Lerp(lineStart.position, lineEnd.position, lerpValue) + Random.insideUnitSphere * desiredRadius);
}
For desiredRadius here, you may want to do some math to make it transition from 0 to 1 to 0 as lerpValue moves from 0.0 to 1.0. I’ll leave that as an exercise for the reader
now for the 20.000$ question.
how do i declare that setposition0 is always lineStart, and the last setposition(last element in the array) is the lineEnd?
so i can add or remove segments(vetrexes) and still have a start and end?
Well, the loop in my sample code sets the first one to Lerp(start, end, 0f), which is the same as just “start”, so that one’s taken care of - if you do the “change the radius from 0 to 1 to 0” thing also mentioned, it’ll be right at the start point. You can do the same with the end by using (float) i / (numPoints - 1) in the lerpValue - this will make the last one’s lerpValue be 1f, which will be the same as “end”.
using UnityEngine;
using System.Collections;
public class Lightning : MonoBehaviour
{
private LineRenderer lineRenderer;
public int numPoints =10;
public Transform lineStart;
public Transform lineEnd;
public float desiredRadius;
public bool isActivated = false;
private void Start()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.positionCount = numPoints;
}
void Update ()
{
if (isActivated == true)
{
for (int i = 0; i < numPoints; i++)
{
float lerpValue = (float)i / numPoints; //if you don't typecast to float here, it'll do the math as integers and come up with 0 every time
lineRenderer.SetPosition(i, Vector3.Lerp(lineStart.position, lineEnd.position, lerpValue) + Random.insideUnitSphere * desiredRadius);
}
lineRenderer.SetPosition(0, lineStart.position);
//lineRenderer.SetPosition(1, Vector3.Lerp(lineStart.position, lineEnd.position, 0.5f) + Random.insideUnitSphere * desiredRadius);
lineRenderer.SetPosition(numPoints - 1, lineEnd.position);
}
else
{ return;
}
}
}