Hello,
I make the game rhythm and I’m forced to use GetSpectrumData() to create objects to the rhythm using the Instantiate()
The problem is that in spite of the script, which @jister help me to write this, slightly helped (reduced the number of objects to 2-3 per beat) but still not formed exactly good one.
What else can I do to make it work well? Edit the music? Another script?
Gif demonstrates the problem:
Script
using UnityEngine;
using System.Collections;
public class FrequencyTresholdSpawner : MonoBehaviour {
//ZMIENNE
//Obiekt
public GameObject objectPrefab;
//treshold
public float spawnThreshold = 0.05f;
//Frequency
public int frequency;
//Window
public FFTWindow fftWindow;
GameObject clone;
//ZMIENNA ILOSC SAMPLI
private float[] samples = new float[4096]; //MUST BE A POWER OF TWO
void Start()
{
}
// Update is called once per frame
void Update ()
{
//GETSPECTRUMDATA
AudioListener.GetSpectrumData(samples, 0, fftWindow);
//Tworzenie Obiektu
Debug.Log(samples[frequency]);
if(samples[frequency] > spawnThreshold)// && clone != null)
{
clone = (GameObject)Instantiate(objectPrefab, new Vector3(Random.Range(-10.0f, 10.0f), 5, 0), Quaternion.identity);
}
else
clone = null;
}
}