How to call Random.insideUnitSphere.x Once
I'm having a lil bit problem about Positioning my Random Lightning Strike every frame. Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//[ExecuteInEditMode]
public class ThunderBehaviour : MonoBehaviour {
public AudioClip[] Thunder;
public AudioSource audsource;
private int _numberofparticles = 0;
private int count;
public ParticleSystem[] partSys1;
public GameObject[] partSys;
public float timer = 8f;
//public float xPos;
//public Vector3 desiredPos;
void Start(){
audsource = GetComponent<AudioSource> ();
StartCoroutine (ThunderSound());
//the scale of the particlesystem
foreach(ParticleSystem sizePart in partSys1){
sizePart.transform.localScale += new Vector3 (70.0f, 0.0f, 130.0f);
//sizePart.transform.localRotation = Quaternion.identity;
}
//xPos = Random.Range (-100.0f, 100.0f);
//desiredPos = new Vector3 (xPos,transform.position.y, transform.position.z);
foreach(GameObject partSystem in partSys){
partSystem.transform.position = new Vector3 (Random.insideUnitSphere.x * 300,partSystem.transform.position.y, partSystem.transform.position.z);
/*if(Vector3.Distance(transform.position, desiredPos) <= 0.01f){
xPos = Random.Range (-100.0f, 100.0f);
desiredPos = new Vector3 (xPos, transform.position.y, transform.position.z);
}*/
}
}
void Update(){
timer -= Time.deltaTime;
if(timer <= 3.0f){
RandomizeLightning ();
RestartTimer ();
}
}
IEnumerator ThunderSound(){
while(true){
//call inside of ParticleSystem[] partSys1 array
foreach(ParticleSystem particleSystem in partSys1){
count = particleSystem.particleCount;
if(count > _numberofparticles && count > _numberofparticles){
audsource.clip = Thunder[Random.Range(0,Thunder.Length)];
audsource.Play ();
for(int i = 0; i < partSys.Length; i++){
if (partSys1 [0].IsAlive () && partSys1 [1].IsAlive()) {
yield return new WaitForSeconds (1);
}
partSys [0].SetActive (false);
partSys [1].SetActive (false);
yield return new WaitForSeconds (audsource.clip.length);
partSys [0].SetActive (true);
partSys [1].SetActive (true);
}
}
}
yield return null;
}
}
public void RestartTimer(){
timer = 8.0f;
}
public void RandomizeLightning(){
foreach(GameObject partSystem in partSys){
partSystem.transform.position = new Vector3 (Random.insideUnitSphere.x * 300,partSystem.transform.position.y, partSystem.transform.position.z);
/*if(Vector3.Distance(transform.position, desiredPos) <= 0.01f){
xPos = Random.Range (-100.0f, 100.0f);
desiredPos = new Vector3 (xPos, transform.position.y, transform.position.z);
}*/
}
}
}
What is happening on my code is that after the first execution of my lightning it will go to the random position then it will move again 2x before my lightning executes again. What i want here is that i want to move the position of lightning after it executes.
Comment
Best Answer
Answer by Ginxx009 · Jan 05, 2018 at 05:54 AM
I just created a function that controls the lightning and the movement of the object. :)