Hello,
I recently found a thread with some code on how to make a lightning strike between two objects…
I am hoping to use something like this as a “weapon” in a project that I am putting together.
As of now, when I attach the script to my gameobject nothing happens. I am obviously missing something.
In my scene, the gameobject that the LightningStrike script is attached to is named Ship and the target gameobject is named target. I added a lineRenderer to the player “ship”.
My hope with this is to have a moving player “ship” following along an iTween path, which strikes a bolt of lighting to another moving gameobject “target” on another iTween path. I have the gameObjects working and following their paths… I’m just a little stuck/confused on how to modify the code I’ve posted below to get it to work on stationary objects such as cubes, or moving gameObjects.
Below is the code that I retyped from the link posted above.
I am very new to coding, so if anyone offers help or suggestions, will you also maybe explain why the change, or what I could explore to get this to sink in a bit.
Thanks ahead of time, I really appreciate it.
~Kory
using UnityEngine;
using System.Collections;
public class LightningStrike : MonoBehaviour {
public GameObject target;
private LineRenderer lineRend;
public float arcLength = 2.0f;
public float arcVariation = 2.0f;
public float inaccuracy = 0.5f;
public float timeOfZap = 0.25f;
public float zapTimer;
void start() {
lineRend = gameObject.GetComponent<LineRenderer> ();
zapTimer = 0;
lineRend.SetVertexCount (1);
}
void update() {
if (zapTimer > 0) {
Vector3 lastPoint = transform.position;
int i =1;
lineRend.SetPosition (0, transform.position);
while (Vector3.Distance(target.transform.position, lastPoint) > 3.0f) {
lineRend.SetVertexCount (i + 1);
Vector3 fwd = target.transform.position - lastPoint;
fwd.Normalize ();
fwd = Randomize (fwd, inaccuracy);
fwd *= Random.Range (arcLength * arcVariation, arcLength);
fwd += lastPoint;
i++;
lastPoint = fwd;
}
lineRend.SetVertexCount (i + 1);
lineRend.SetPosition (i, target.transform.position);
//lightTrace.TraceLight (gameObject.transform.position, target.transform.position);
zapTimer = zapTimer - Time.deltaTime;
} else
lineRend.SetVertexCount (1);
}
private Vector3 Randomize (Vector3 newVector, float devation) {
newVector += new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * devation;
newVector.Normalize();
return newVector;
}
public void ZapTarget( GameObject newTarget){
target = newTarget;
zapTimer = timeOfZap;
}
}