public void Initilize(Vector2 start, Vector2 end, GameObject boltPrefab) { Start = start; End = end; GameObject mainBoltObj = (GameObject)GameObject.Instantiate(boltPrefab); LightningBolt mainBoltComponent = mainBoltObj.GetComponent <LightningBolt> (); mainBoltComponent.Initilize(5); mainBoltComponent.ActivateBolt(start, end, Color.white, 1f); boltsObj.Add(mainBoltObj); int numBranches = Random.Range(3, 6); Vector2 diff = end - start; List <float> branchPoints = new List <float> (); for (int i = 0; i < numBranches; i++) { branchPoints.Add(Random.value); } branchPoints.Sort(); for (int i = 0; i < branchPoints.Count; i++) { Vector2 boltStart = mainBoltComponent.GetPoint(branchPoints [i]); Quaternion rot = Quaternion.AngleAxis(30 * ((i & 1) == 0 ? 1 : -1), new Vector3(0, 0, 1)); Vector2 adjust = rot * (Random.Range(0.5f, 0.75f) * diff * (1 - branchPoints [i])); Vector2 boltEnd = adjust + boltStart; GameObject boltObj = (GameObject)GameObject.Instantiate(boltPrefab); LightningBolt boltComponent = boltObj.GetComponent <LightningBolt> (); boltObj.transform.SetParent(this.transform); boltComponent.Initilize(5); Color color; color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1); boltComponent.ActivateBolt(boltStart, boltEnd, color, 1f); boltsObj.Add(boltObj); } }
public void Initialize(Vector2 start, Vector2 end, GameObject boltPrefab) { //store start and end positions Start = start; End = end; //create the main bolt from our bolt prefab GameObject mainBoltObj = (GameObject)GameObject.Instantiate(boltPrefab); //get the LightningBolt component LightningBolt mainBoltComponent = mainBoltObj.GetComponent <LightningBolt>(); //initialize our bolt with a max of 5 segments mainBoltComponent.Initialize(5); //activate the bolt with our position data mainBoltComponent.ActivateBolt(start, end, Color.white, 1f); //add it to our list boltsObj.Add(mainBoltObj); //randomly determine how many sub branches there will be (3-6) int numBranches = Random.Range(3, 6); //calculate the difference between our start and end points Vector2 diff = end - start; // pick a bunch of random points between 0 and 1 and sort them List <float> branchPoints = new List <float>(); for (int i = 0; i < numBranches; i++) { branchPoints.Add(Random.value); } branchPoints.Sort(); //go through those points for (int i = 0; i < branchPoints.Count; i++) { // Bolt.GetPoint() gets the position of the lightning bolt based on the percentage passed in (0 = start of bolt, 1 = end) Vector2 boltStart = mainBoltComponent.GetPoint(branchPoints[i]); //get rotation of 30 degrees. Alternate between rotating left and right. (i & 1 will be true for all odd numbers...yay bitwise operators!) Quaternion rot = Quaternion.AngleAxis(30 * ((i & 1) == 0 ? 1 : -1), new Vector3(0, 0, 1)); //calculate how much to adjust for our end position Vector2 adjust = rot * (Random.Range(.5f, .75f) * diff * (1 - branchPoints[i])); //get the end position Vector2 boltEnd = adjust + boltStart; //instantiate from our bolt prefab GameObject boltObj = (GameObject)GameObject.Instantiate(boltPrefab); //get the LightningBolt component LightningBolt boltComponent = boltObj.GetComponent <LightningBolt>(); //initialize our bolt with a max of 5 segments boltComponent.Initialize(5); //activate the bolt with our position data boltComponent.ActivateBolt(boltStart, boltEnd, Color.white, 1f); //add it to the list boltsObj.Add(boltObj); } }