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 TMR · Nov 11, 2013 at 06:39 AM · c#collisionaudio

Sounds triggering too fast

So, I've posted about this before, but I've tried every suggestion I've been given with the same issue always arising so I feel like something is just missing from the code at this point. I have a walking sound array being triggered by the arrow keys when I press them down, the sound clips in the array will change depending on which kind of surface (ex. wood, grass, dirt, etc.) my character is on and I have the sounds being changed via OnCollisionColliderHit using tags as a reference point for the change. Everything is being triggered on the right surfaces but the only issue is that the sound is being called every frame-ish. I don't have anything in the Update function so not sure what's going on. I'm still quite new to scripting so apologies if I'm making a rookie mistake.

 using UnityEngine;
 using System.Collections;
 
 public class WormWalk : MonoBehaviour 
 {
     public AudioSource Audio;
         
     public AudioClip[] walkingSounds;
     
     public int pitchRandomness = 1;
     
     // Use this for initialization
     void Start () 
     {
         Audio = (AudioSource)gameObject.GetComponent ("AudioSource");
     }
     
     
     // Update is called once per frame
     void Update () 
     {
         
     }
     
     void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
         {
             Audio.Play();
         }
         else
         {
             Audio.Stop ();
         }
         
         if (hit.gameObject.tag == "groundFloor")
         {
             print("Ground");
             Audio.clip = walkingSounds[0];
         }
 
         if (hit.gameObject.tag == "woodFloor")
         {
             print("Wood");
             Audio.clip = walkingSounds[1];
         }
     
         if (hit.gameObject.tag == "stoneFloor")
         {
             print("Stone");
             Audio.clip = walkingSounds[2];
         } 
     
         if (hit.gameObject.tag == "templeFloor")
         {
             print("Temple");
             Audio.clip = walkingSounds[3];
         }
     }
 }
Comment
Add comment · Show 3
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 mattssonon · Nov 11, 2013 at 08:34 AM 0
Share

Everything is being triggered on the right surfaces but the only issue is that the sound is being called every frame-ish.

So the issue is that sound is playing continously?

avatar image TMR · Nov 11, 2013 at 03:11 PM 0
Share

In a sense yes. I have a worm character and I'm just wanting a few loops I made of, for example, the character sliding on top of grass to just play the loop and keep looping as I have the keys down. The issue is that the loop isn't playing to completion, it's a 10 second loop but it's only playing the first 0.1 seconds and rapidly (one after another). So I'm assu$$anonymous$$g my solution is to just get the loop to play all the way through and loop properly. I'm just not sure how to go about that, I've dug through many different methods but not having any luck.

avatar image mattssonon · Nov 11, 2013 at 03:33 PM 0
Share

Have you checked if Audio.Play(); is being called constantly?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Moor · Nov 21, 2013 at 05:38 AM

try this

  if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
 {
 Audio.Play();
 }
 else if(Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
 {
 Audio.Stop ();
 }
Comment
Add comment · Show 4 · 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 TMR · Nov 21, 2013 at 05:58 AM 0
Share

Just plugged that code in but still having the sound issue :/ but thank you very much for the suggestion!

avatar image Moor · Nov 21, 2013 at 06:14 AM 1
Share

take all the print statement out and use

   void OnControllerColliderHit(ControllerColliderHit hit)
     {
     if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
     {
         
     else if (hit.gameObject.tag == "groundFloor")
     {
   audio.PlayOneShot( walkingSounds[0], 1.0);
     }
      
    else if (hit.gameObject.tag == "woodFloor")
     {
   audio.PlayOneShot( walkingSounds[1], 1.0);
     }
      
    else if (hit.gameObject.tag == "stoneFloor")
     {
   audio.PlayOneShot( walkingSounds[2], 1.0);
     }
      
    else if (hit.gameObject.tag == "templeFloor")
     {
   audio.PlayOneShot( walkingSounds[3], 1.0);
     }
     }
     }
 else if(Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
 {
 Audio.Stop ();
 }
avatar image TMR · Nov 21, 2013 at 06:41 AM 0
Share

Thanks again for the suggestion! On an upside the overlapping sound issue sounds different now but I'm still having an issue overall with not being able to just loop a single sound (per surface)

avatar image Moor · Nov 21, 2013 at 06:53 AM 1
Share
   if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow) && !audio.isPlaying){
     audio.clip = walkingSounds[i];
     audio.Play();
   }
avatar image
0

Answer by static_cast · Nov 11, 2013 at 04:18 PM

 Audio.pitch = (1);

Apparently audio.pitch changes the speed of your sound, so try altering it to below your pitch to see if it plays correctly.

Comment
Add comment · Show 1 · 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 TMR · Nov 12, 2013 at 05:48 AM 0
Share

Gave it a shot but no luck, thanks for the suggestion though!

avatar image
0

Answer by loboclerk · Nov 12, 2013 at 07:21 AM

It looks like OnControllerColliderHit is being called repeatedly.

Maybe just run Audio.Play() each time the clip is changed.

 public AudioClip prevAudio = null;
 
 // for each instance where you assign Audio.clip
 
 prevAudio = Audio.clip;
 Audio.clip = xxxxxxxxx;
 
 
 if (prevAudio!=Audio.clip) {
     Audio.Play();
 }
 else {
     prevAudio = null;
     Audio.Stop();
 }
Comment
Add comment · Show 1 · 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 TMR · Nov 16, 2013 at 06:19 AM 0
Share

Is there another way I could get the sounds to switch via a tag-reference method or some other means? And I don't think I fully understand the code you posted (I'm still new to coding, I tried to connect the dots in my head as to how to plug this code into what I currently have going on and can't think of a good way to do so).

avatar image
0

Answer by Remingsworth · Apr 01, 2015 at 12:28 PM

I too agree, as loboclerk suggested, that the audio is being called repeatedly. If this is the case then perhaps try using a bool to prevent the audio from playing if it is already in the process of playing. A .cs and .js of this can be found in a similar post here: http://answers.unity3d.com/questions/301797/audioplayoneshot-update-mess.html

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

20 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 avatar image avatar image avatar image

Related Questions

Need help with scripting bug fix. pleease 0 Answers

Yet another Audio on Collision issue 1 Answer

Collision sounds doubling up. 1 Answer

Problem Detecting 2D Collisions 1 Answer

Multiple Cars not working 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