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 p_unity · Apr 12, 2013 at 11:56 AM · audio sourceplayoneshotdistorted

Sound Clip Heavy Distortion

Hey guys first post.. still a newbie. trying to call an audio clip to play when the player hits a certain collider, this instance a door to a certain building, door already has an animation on it. The sound clip itself is fine,but when i call it to be played, it seems like there is multiple instances of the clip, or it is being asked to play over and over? as its very loud and distoreted, so it seems like a coding error i guess??

im opening the doors using a raycast, so i have just asked the sound clip to be called when this raycast collides with "BuildingDoor"

function Update () { var challengeAudio : AudioClip; var hit:RaycastHit;

 //Check for collison with var myRaycast
 if (Physics.Raycast(transform.position, transform.forward, hit, myRaycast))
 
 {
            if (hit.collider.gameObject.name == "IndustrialEntrance")
     {
         audio.PlayOneShot(challengeAudio);
         hit.collider.gameObject.animation.Play("interiorDoorSwing");
     }

}

but something has clearly gone wrong....

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

2 Replies

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

Answer by ByteSheep · Apr 12, 2013 at 12:32 PM

Add a check to see if the audio is already being played so you don't call it multiple times.

 var challengeAudio : AudioClip; 
 function Update () { 
 
     var hit:RaycastHit;
 
     //Check for collison with var myRaycast
     if (Physics.Raycast(transform.position, transform.forward, hit, myRaycast)) {
       if (!audio.isPlaying && hit.collider.gameObject.name == "IndustrialEntrance")
       {
         audio.PlayOneShot(challengeAudio);
         hit.collider.gameObject.animation.Play("interiorDoorSwing");
       }
 }

You could do the same thing using a variable to check if the door is currently being opened and only if it is false play the sound.

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 p_unity · Apr 12, 2013 at 12:47 PM 0
Share

thanks for your answer bud, i tried that solution, and its less distorted, however it is still overlapping itself, and not working how it should. if i set a var isOpen of type bool to false, and make it true when the player hits the door, and then call a playSound function which just has the audio.PlayOneShot call inside it if(isOpen), is that what ur getting at? isnt that just applying the same logic as this solution though? but in a different way?

avatar image ByteSheep · Apr 12, 2013 at 12:57 PM 0
Share

Well the problem with using audio.isPlaying is that if the audio has finished playing and you are still facing the door, then it will play again.
By using a variable to keep track of whether the door has been opened or not will make sure the sound isn't played again.
You could also ins$$anonymous$$d try adding a check to see if the player pressed a certain key to open the door.
Give this a try and see if you still get the distorted audio (open door by pressing "e" key):

 var challengeAudio : AudioClip;
 function Update () {
      
   var hit:RaycastHit;
   
   if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
   {
 
     //Check for collison with var myRaycast
     if (Physics.Raycast(transform.position, transform.forward, hit, myRaycast)) {
       if (!audio.isPlaying && hit.collider.gameObject.name == "IndustrialEntrance")
       {
         audio.PlayOneShot(challengeAudio);
         hit.collider.gameObject.animation.Play("interiorDoorSwing");
       }
     }
 
   }
 }
avatar image p_unity · Apr 12, 2013 at 02:00 PM 0
Share

$$anonymous$$erry Christmas to you too mate. that is working, needing input to open doors feels better anyway. This does work, but it will only work on gameObjects requiring input, i am having the same problem with a lot of my audio clips, like playing music when player is spotted by enemy.

ill try imnickb's option and see what happens. thanks a lot though

avatar image
1

Answer by imnickb · Apr 12, 2013 at 01:14 PM

You could make a bool, like shouldSudioPlay and set it to true before you get to the door. If the bool is true let the audio play and then immediately set it to false. After the door is shut set it back to true in case you want to go back through the door and you need the audio to play again. Something like this:

         function Start ()
         {
             var audioShouldPlay = true;
         }
         function Update () 
         { 
              var challengeAudio : AudioClip;
              var hit:RaycastHit;
     
              //Check for collison with var myRaycast
              if (Physics.Raycast(transform.position, transform.forward, hit, myRaycast))
              {
                  if (hit.collider.gameObject.name == "IndustrialEntrance")
                  {
                     if (audioShouldPlay)
                     {
                         audioShouldPlay = false;
                         audio.PlayOneShot(challengeAudio);
                     }
                     hit.collider.gameObject.animation.Play("interiorDoorSwing");
                  }
              }
         }
         
Comment
Add comment · Show 5 · 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 p_unity · Apr 12, 2013 at 02:09 PM 0
Share

top man nickb, it works, and i have applied this logic to other objects that use OnCOntrollerColliderHit and OnCollison functions ins$$anonymous$$d of raycast hit, and the distortion is no more.

great support guys thanks again.

avatar image p_unity · Apr 12, 2013 at 02:10 PM 0
Share

these are both correct answers, but i can only mark one.

avatar image imnickb · Apr 12, 2013 at 02:14 PM 0
Share

Then mark $$anonymous$$e! Hah! It's alright, I don't care either way. Just make sure you remember to switch your bools back to true when you're done with stuff. If you don't you'll be wondering why half of your audio's not working after you run the game for a bit.

avatar image p_unity · Apr 12, 2013 at 02:21 PM 0
Share

ill try, haha.. so inside the first if(Physics.Raycast.... outside the collision detection set it back to true?

avatar image imnickb · Apr 12, 2013 at 02:27 PM 0
Share

It'll be in different places depending on what the situation is. In the door situation just make sure you set it back to true after the door is closed, so you could play your door close animation and set it to true after that. I don't know how your game works but if you cant go back through the door, and all of your other doors are new instances, then you may be fine to just leave it and not set it back. GoodLuck!

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

13 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

Related Questions

Play multiple audio clips from one object 1 Answer

how to PlayOneShot from a OnCollisionEnter wiith a RaycastHit /Collider>Raycast 1 Answer

It is possible to have a random audio pitch with one AudioSource and PlayOneShot? 3 Answers

How to fix my reload sound? 0 Answers

Lower volume when another audio source plays 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