/* Author: Michael Cullen * mcullen.me * Use: Script is placed on Camera/Rigidbody with an AudioSource to play foley effects in Unity scene * */ using UnityEngine; using System.Collections; public class AudioPlayer : MonoBehaviour { public AudioClip[] m_arrClips; // an array the sounds that will be selected from. public AudioSource m_audioSource; public float lowPitchRange = .9f; //The lowest a sound effect will be randomly pitched. public float highPitchRange = 1.2f; //The highest a sound effect will be randomly pitched. private float randomPitch; // Use this for initialization void Start () { m_audioSource = GetComponent (); } // Update is called once per frame void Update () { } public void PlaySoundAtIndex (int idx){ m_audioSource.clip = m_arrClips[idx]; //Choose a random pitch to play back our clip at between our high and low pitch ranges. float randomPitch = Random.Range(lowPitchRange, highPitchRange); //Set the pitch of the audio source to the randomly chosen pitch. m_audioSource.pitch = randomPitch; //Debug.Log(m_audioSource.pitch); //Play the clip.*/ m_audioSource.PlayOneShot(m_audioSource.clip); } }