Can't figure out how to play a random audio clip when player collides with enemy
I thought this would have been the easiest part of making a game! I have been researching and trying different codes for days and have not come anywhere close to figuring this out. I have an array of 6 sounds. I want 1 of the 6 sounds to randomly play when the player collides with an enemy. I know how to do this part, but the audio just won't play. All the videos and articles I read are about setting an audio source on the object and playing "one" sound when a trigger occurs. My enemies are prefabs that spawn and destroy themselves after some time. I also have a particle system that triggers when the enemy is destroyed. Why does the particle system work, but the sound won't? If I even try to comment out everything in the if statements (in the ontriggerenter2d method) and leave only the audio file, no sounds are heard. There will be lots of collisions in my game and I want to add a variety of sounds. This is driving me crazy!
Here is my current code:
using UnityEngine;
using System.Collections;
using UnityEngine .Audio ;
public class FlipHead : MonoBehaviour {
public float speed;
private SpriteRenderer mySpriteRenderer;
public GameObject particle;
public GameObject particle2;
public AudioClip[] clips;
public AudioMixerGroup output;
public AudioSource noise;
//private float someScale;
//private float direction, _posX;
void Awake ()
{
mySpriteRenderer = GetComponent<SpriteRenderer>();
//direction = 1;
//someScale = transform.localScale.x;
}
void Start(){
}
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
}
if(mySpriteRenderer != null)
{
if(Input.GetTouch (0).deltaPosition .x<0)
{
// flip the sprite
mySpriteRenderer.flipX = true;
}
if(Input.GetTouch (0).deltaPosition.x >0)
{
// flip the sprite
mySpriteRenderer.flipX = false;
}
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Running Player") {
GameObject clonedParticle;
GameObject sound;
clonedParticle =Instantiate (particle, transform.position, transform.rotation) as GameObject ;
int randomClip = Random.Range (0, clips.Length);
sound=Instantiate (clips[randomClip ], transform.position, transform.rotation)as GameObject ;
//noise.PlayOneShot (clips [randomClip]);
Destroy (other.gameObject);
Destroy (sound, clips [randomClip].length);
Destroy (clonedParticle, 2);
}
if (other.tag == "Running Ref") {
//int soundIndex = Random.Range (0, BiteSounds .Length);
GameObject clonedParticle2;
clonedParticle2 =Instantiate (particle2, transform.position, transform.rotation) as GameObject ;
Destroy (other.gameObject);
Destroy (clonedParticle2,2);
}
}
/*void PlaySound(){
int randomClip = Random.Range (0, clips.Length);
AudioSource source = gameObject.AddComponent <AudioSource > ();
source.clip = clips [randomClip];
source.outputAudioMixerGroup = output;
source.Play();
Destroy (source,clips [randomClip ].length );
}*/
}
Answer by Cherno · May 02, 2016 at 11:02 AM
if (other.tag == "Running Player") {
GameObject sound = new GameObject();
sound.transform.position = transform.position;
int randomClip = Random.Range (0, clips.Length);
AudioSource audioSource = sound.AddComponent<AudioSource>();
audioSource.clip = clips[randomClip ];
audioSource.Play();
Destroy (sound, clips [randomClip].length);
}
You are amazing sir! This works great!! Thank you so much!!!
Your answer
Follow this Question
Related Questions
Audio Trigger 2D in C#? 1 Answer
Trigger detects player collision and play specific audio from array 1 Answer
FPS Audio Issue 1 Answer
IndexOutOfRangeException error 1 Answer