This video shows how to move Object by sound in Unity
Please help me, what I need to change in code to spawn 3D Object by sound in Unity.
Code from the video:
using UnityEngine;
using System.Collections;
public class SoundSC : MonoBehaviour {
public float sensitivity = 100;
public float loudness=0;
AudioSource _audio;
// Use this for initialization
void Start () {
_audio = GetComponent<AudioSource> ();
_audio.clip = Microphone.Start (null, true, 10, 44100);
_audio.loop = true;
_audio.mute = true;
while (!(Microphone.GetPosition(null) > 0)) {}
_audio.Play ();
}
// Update is called once per frame
void Update () {
loudness = GetAveragedVolume () * sensitivity;
if (loudness > 8)
this.GetComponent<Rigidbody2D>().velocity = new Vector2(this.GetComponent<Rigidbody2D>().velocity.x, 4);
}
//
float GetAveragedVolume()
{
float[] data = new float[256];
float a = 0;
_audio.GetOutputData (data, 0);
foreach(float s in data)
{
a+=Mathf.Abs(s);
}
return a / 256;
}
}