- Home /
Getting audio to play on collision
I'm fairly new to Unity 3D and game programming (high school class so few months of slow pace and self-teaching under my belt). I tried to "make" a game outside of the classroom( um its TornadoTwin's Worminator Tutorial game except I wanted to tweak it a little bit to make it my own)...and it isn't WORKING If you haven't seen the tutorials then just know that turrets and the worm shoot a sphere from the spawnPoint in front of their head tagged as fireBall and/or wormProjectile. It has a Sphere Collider and has isTrigger checked as well as a rigid body. I am trying to get an audio clip (a crackling fire sound) to play when the turrets get destroyed by my worm's fireball. Note: the OnFire script has // b/c it kept giving me compiler errors, they weren't working, etc. That script is also is a little jumbled so not everything is connected. Some of it is there b/c I know it can get audio to play but don't know how to use them/slash wouldn't work with the function. Here are my scripts: TurrentCollision (JavaScript)
var explosion: Transform;
(added separately from TornadoTwins for audio to work)
var gotHit: AudioClip;
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == "wormProjectile")
{
Destroy(hit.gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
(me/separate from Tornado)
audio.PlayOneShot(gotHit);
Destroy(gameObject);
}
}
OnFire (JavaScript) (other attempt b4 I realized I could just use the Turret Collission Script)
var fire: AudioClip;
function OnTriggerEnter( hit : Collider)
{
if(hit.gameObject.tag == "fireBall")
{
audio.PlayOneShot(fire);
}
}
// Play impact audio clip when colliding with something
//var impact : AudioClip;
<p>//function OnCollisionEnter (collision : Collision) </p>
<p>//{
//if(collision.gameObject.tag =="wormProjectile")</p>
<p>//{</p>
<p>//for (var impact : ContactPoint in collision.contacts) </p>
<p>//{</p>
<p>//audio.PlayOneShot(fire);
//audio.Play();
//}
//}
//}</p>
<p>//{
//var impact = true;
//audio.Play();
//audio.PlayOneShot(fire);
//function OnTriggerEnter( hit : Collider)
//{
//if(hit.gameObject.tag == "wormProjectile")
//{
// audio.PlayOneShot(impact);
//}</p>
<p>//}</p>
@Jason 12, I'd like to hit you on the head with a frying pan. First, you forgot to hit the "code" button. Next, there was a bunch of HT$$anonymous$$L marks in it. I had to go through your question and get rid of most of them...
Answer by Jason 12 · Dec 12, 2010 at 01:23 AM
I figured it out! =D
Turret Collision:
var explosion: Transform; var expSound: AudioClip; var fireSound : AudioClip;
function OnTriggerEnter( hit : Collider)
{
if(hit.gameObject.tag == "wormProjectile")
{
AudioSource.PlayClipAtPoint(fireSound, Vector3 (1.995417,2.282711,-1.524732));
AudioSource.PlayClipAtPoint(expSound, Vector3 (1.995417,2.282711,-1.524732));
Destroy(hit.gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Answer by Karo · May 05, 2011 at 01:45 PM
Hey I had same problem, I know it is a long time you post this. I found this post on google, I did not like that you had to give all your turrets vector 3 position. I have lot of them in my game. Here is better way of doing this, you just delay the time after sound and before the explosion.
var explosion : Transform;
var sfxHit: AudioClip;
function OnTriggerEnter ( hit : Collider) {
if(hit.gameObject.tag == "wormProjectile")
{
audio.PlayOneShot(sfxHit);
yield WaitForSeconds (0.4);
Destroy(hit.gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Answer by witchking409 · Jun 23, 2013 at 04:12 AM
For those using c#
To play audio on collision with a destroyed object
public AudioClip coindAudio;
void OnTriggerEnter(Collider cc)
{
if(gameObject.tag == "coin")
{
codeGUI.coin++;
AudioSource.PlayClipAtPoint(coindAudio,gameObject.transform.position);
}
}
}
Your answer
Follow this Question
Related Questions
Sound on collision 3 Answers
When Audio is played through script it buzzes and glitches 2 Answers
2nd Audio Clip Not Playing 0 Answers
How Do I Get This To Play Sound? 0 Answers
Footsteps... Please Help 1 Answer