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
1
Question by Knoxley · Jul 30, 2013 at 05:37 AM · javascriptcollisiontriggersoundboolean

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

Comment
Add comment
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

3 Replies

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

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;
     
     }
     
     
 }
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 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;
             }
         }
     }
 }
Comment
Add comment · Show 3 · 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 gregzo · Jul 30, 2013 at 05:56 AM 0
Share

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

avatar image Lovrenc · Jul 30, 2013 at 06:02 AM 0
Share

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

avatar image gregzo · Jul 30, 2013 at 06:15 AM 0
Share

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

avatar image
0

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.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sound plays even though on awake is unticked 1 Answer

How to get sound to play on barrel Explosion 1 Answer

trigger is not working 0 Answers

Collecting Array items in order 1 Answer

Making a sound play only once with a Boolean variable in javascript 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