Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jason 12 · Dec 09, 2010 at 06:28 AM · javascriptcollisionaudioaudioclipaudioplay

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>
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireDude67 · Jun 16, 2011 at 04:23 AM 0
Share

@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...

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);    
     }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
}

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
             }
             
         
         }
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges