Hello, I am making a Rhythm game within Unity; and in this, I have 3 spawners, each spawning beats which can be removed once they enter a trigger and the player presses a button. However, now I have them actually spawning to music I have bizarre issues where all 3 spawners either A) Spawn at the same time, and stop spawning at all 30 seconds or so in, and B) Spawn far too many objects at once, causing blockages and what not, balls spiralling all over the screen.
So my question to this is A) How would I go about delaying these spawners randomly, or B) Is there a better fix for this than that?
Note; I do have a random delay but it doesn’t work in this version, as changing the values will only cause it to spawn far too many spheres/beats.
using UnityEngine;
using System.Collections;
public class beat : MonoBehaviour {
public Transform target;
public int minRange;
public bool follow;
private float speed;
public Rigidbody projectile;
private float cooldown = 0.5f;
public int detail = 500;
public float amplitude = 0.1f;
public float minValue = 1.0f;
void Start () {
}
void Update(){
cooldown -= Time.deltaTime;
float[] info = new float[detail];
AudioListener.GetOutputData(info, 0);
float packagedData = 0.0f;
for(int x = 0; x < info.Length; x++)
{
packagedData += System.Math.Abs(info[x]);
}
if (packagedData/info.Length > 0.5f) {
cooldown += Random.Range(1,3);
Rigidbody clone;
clone = Instantiate (projectile, transform.position, transform.rotation) as Rigidbody; //This seems to create the clone
clone.velocity = transform.TransformDirection(Vector3.forward * 40); //This details the velocity of the projectile
}
}
}
Cheers!
Thanks! Seems to help out, but do you have any ideas how I could make it spawn quicker? At the moment it spawns fine, but they're really far apart. Just a case of changing values you reckon?
– gamedev8