- Home /
Alternate between two Audio clips on collision
I have a ball colliding with objects on the screen. Each time the ball collides, it plays a ping sound. However, I want to alternate between two different ping sounds each time it collides. For example, when the ball hits an object, I want Sound A to play, when it hits another object, I want Sound B to play. Each time it collides, it needs to alternate to the other sound.
Thank you
Answer by Knoxley · Jul 30, 2013 at 10:24 PM
Here is what I ended up with. All I really needed to know was that it needed to loop through an array. I had gotten boolean into my head for some reason and was unable to think my way out of that way of thinking.
Thanks for all your help.
//Ball Bing sound effects
var Bing_03 : AudioClip;
var SIBings : AudioClip[];
var arrayCount : int = 0;
function OnCollisionEnter(collision : Collision)
{
//Plays Standard Bing
if(collision.gameObject.tag == "Wall")
{
audio.PlayOneShot(Bing_03, .75);
}
//Plays Cycling Bing
if(collision.gameObject.tag == "SI")
{
audio.PlayOneShot(SIBings[arrayCount], 1);
if(++arrayCount == SIBings.length)
arrayCount = 0;
}
}
Answer by tw1st3d · Jul 30, 2013 at 05:52 AM
using UnityEngine;
using System.Collections;
class ChangeSound : MonoBehavior
{
public AudioClip clip1, clip2;
void OnCollisionEnter(Collision e)
{
if(e typeof GameObject)
{
if(e.tag == "ball")
{
audio.Play(0);
if(audio.clip == clip1)
audio.clip = clip2;
else
audio.clip = clip1;
}
}
}
}
Apoligies for the down vote, this practise of simply writing scripts for beginners is simply score hunting and doesn't help to learn. Teach a man how to fish...
Problem is, at least to my experience, most newbies who come here have no intention on learning anything. Post anything that has more than 5 lines of explanation or link to good documentation...
tl;dr
Code is a bit incorrect : if 2 collisions happen in a time that is shorter than the played AudioClip, sound will get cut off as clips are switched. Use PlayOneShot ins$$anonymous$$d, or even better, 2 or more audiosources with s$$anonymous$$l behaviours. ( PlayOneShot should do fine to start with ).
Answer by gregzo · Jul 30, 2013 at 07:08 AM
[RequireComponent ( typeof ( AudioSource ) ) ] //make sure we have an AudioSource
using UnityEngine;
using System.Collections;
public class BallCollisionAudio
{
public AudioClip[] collisionClips; //assign any nb of clips in the inspector
public float impactToVolumeRatio = .2f; // How loud will the sound play proportionally to impact, try different values
public float minCollisionVolume = .1f;
private int _nextClipIndex = 0;
void OnCollisionEnter ( Collision col )
{
if ( col.gameObject.tag == "CollisionTag" ) //not needed if your balls play sound whatever they collide with
{
float volume = col.relativeVelocity.magnitude * impactToVolumeRatio; //calculate relative volume
volume = Mathf.Clamp ( volume, minCollisionVolume, 1f ); // clamp to make sure it doesn't exceed 1, and is at least minCollisionVolume
audio.PlayOneShot ( collisionClips[_nextClipIndex], volume ); //use PlayOneShot and not Play to avoid cutting off clips if collisions happen in rapid succession
_nextClipIndex = ( _nextClipIndex + 1 ) % collisionClips.Length; //increment the index and make it wrap
}
}
}
Tantrum over, trying a constructive reply.