Question by
$$anonymous$$ · Mar 27, 2017 at 05:50 PM ·
c#audioaudiosourceaudio.play
Playing multiple sounds at the same time?
I'm working on a 2D 8-ball pool game. In my game, each pool ball has a AudioSource attached. This AudioSource plays a hit sound with a random pitch every time it collides with another ball. However, as far as I can tell, Unity is only playing one sound at a time. I want sounds to overlap each other rather than play in rapid succession. Here's my code:
public AudioClip ballHit;
public float audioSpeedModifier;
public AudioClip ballHit;
Rigidbody2D rBody;
AudioSource audioSource;
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Ball" || col.gameObject.tag == "CueBall"){
audioSource.clip = ballHit;
audioSource.volume = rBody.velocity.magnitude * audioSpeedModifier;
float random = Random.Range (0.7f , 1.3f);
//Debug.Log (random);
audioSource.pitch = random;
audioSource.Play ();
}
}
Thanks.
Comment
Best Answer
Answer by IvovdMarel · Mar 27, 2017 at 06:04 PM
The best way to get around this is to use audioSource.PlayOneShot() as opposed to audioSource.Play(). Keep in mind that PlayOneShot requires an audioClip parameter, so you should make a
public AudioClip hitClip;
and call
audioSource.PlayOneShot(hitClip);
Your answer
Follow this Question
Related Questions
Pick Up Sound 0 Answers
Stop AudioSource on Trigger!!! 1 Answer
Why won't my second Audio Source play? 0 Answers
Audio Endless Loop c# 1 Answer