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 Noveria · May 20, 2015 at 09:38 PM · c#javascriptcollisionaudioclip

Play noise when landing on the ground unity c#

I have a player object using a character controller and a capsule collider.

I have a ground object that is made up of sveral cubes (as platforms) that each have a box collider.

The ground has a Rigidbody.

The audio plays fine when jumping around on the first block but when attempting to land on the second as the player approaches the ground a spam of noise occurs(the audio clips being played multiple times at the same time).

All of my collision boxes are setup to the correct size but the noise occurs before the player touches the collision box or the ground?

Here is what I have so far:

 [RequireComponent(typeof(AudioSource))]
 public class Landing : MonoBehaviour
 {
 
     public AudioClip[] list;
     public AudioClip pl_wade1;
     public AudioClip jumplanding4;
     AudioSource audio;
     CharacterController characterController;
     int number;
 
     void Start () 
     {
         audio = GetComponent<AudioSource>();
         characterController = GetComponent<CharacterController>();
         //Loading the items into the array
         list =  new AudioClip[]
         {
             (AudioClip)Resources.Load("Sound/jumplanding1"),
             (AudioClip)Resources.Load("Sound/jumplanding2"), 
             (AudioClip)Resources.Load("Sound/jumplanding3"), 
             (AudioClip)Resources.Load("Sound/jumplanding4")
         };
     }
 
     void OnCollisionEnter (Collision col)
     {
         int number;
         number = Random.Range (0, list.Length);
 
         if (col.gameObject.name == "Ground") 
         {
             if(!audio.isPlaying)
             {
                 audio.PlayOneShot(list[number], 0.1f);
             }
         }
     }
 }

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 DanGoldstein91 · May 21, 2015 at 01:16 AM 0
Share

Hi. I would suggest changing your question title to a more accurate one, as you already know how to play a noise when landing on the ground. $$anonymous$$aybe a subject like "Prevent audio instances from occurring multiple times in 1 OnCollisionEnter" or "prevent collider from registering multiple times per frame".

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DanGoldstein91 · May 21, 2015 at 06:39 AM

I ran into this same issue last month, I used a bool to only allow sounds to fire off once per collision. I'm not sure what audio.isPlaying does so I added my bool alongside it. I think this might solve your problem:

  void OnCollisionEnter (Collision col)
  {
      int number;
      number = Random.Range (0, list.Length);
      bool canPlayJumpLandingSound;

      if (col.gameObject.name == "Ground") 
      {
          canPlayJumpLandingSound = true;
          if(!audio.isPlaying && canPlayJumpLandingSound == true)
          {
              canPlayJumpLandingSound = false;
              audio.PlayOneShot(list[number], 0.1f);
          }
      }
  }

If for some reason that still doesn't work, you can start a coroutine that only plays sound and enables canPlayJumpLandingSound after a Yield return new WaitForSeconds (0.02f). It would look something like this:

  void OnCollisionEnter (Collision col)
  {
      int number;
      number = Random.Range (0, list.Length);

      if (col.gameObject.name == "Ground") 
      {
          canPlayJumpLandingSound = true;
          if(!audio.isPlaying && canPlayJumpLandingSound == true)
          {
              StartCoroutine(PlayLandingSound())
          }
      }
  }

 IEnumerator PlayLandingSound ()
 {                 
     canPlayJumpLandingSound = false;
     audio.PlayOneShot(list[number], 0.1f);
     yield return new WaitForSeconds (0.02f)
     canPlayJumpLandingSound = true;
 }

This coroutine was my plan B as a workaround for the spamming on contact. I tested it and it worked great with a delay of 0.02f, but further testing showed the bool by itself got the job done in my case. Hope that helps.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why is my score not being updated when the collision happens? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Playing audio clip 2 Answers

Object following a path and colliding with other objects with physics. 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