Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Buzzing at start of audio clip (solved)

Discussion in 'Scripting' started by Imari, Jul 5, 2010.

  1. Imari

    Imari

    Joined:
    May 26, 2010
    Posts:
    24
    I'm trying to use the following code to play a sound before going to another scene. The problem is that there's an odd buzzing sound played at the beginning of the audio clip. This happens no matter which sound I use. Any suggestions or solutions would be much appreciated.

    Code (csharp):
    1. var rayLength = 22;
    2.  
    3. function Update(){
    4. if (Input.GetMouseButton(0)) {
    5.        
    6. var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7. var hit: RaycastHit;
    8.  
    9. if (Physics.Raycast(ray, hit, rayLength)) {
    10. audio.Play();
    11. Invoke("NewLevelFunc", audio.clip.length);
    12. }
    13. }
    14. }
    15.  
    16. function NewLevelFunc() {
    17.     Application.LoadLevel ("Green");
    18. }
    P.S.- Many thanks to those generous people who have posted code to get me even this far.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the buzzing sound is by design. You tell it to play the sound but nowhere do you set a state or check one to ensure that the level switch is not already happening so what you do is actually fire the sound and level load dozens of times until the actual level switch happens.
     
  3. Imari

    Imari

    Joined:
    May 26, 2010
    Posts:
    24
    Ah, thank you, dreamora!

    I thought that
    Code (csharp):
    1. Invoke("NewLevelFunc", audio.clip.length);
    prevented the the level from trying to load until after the end of the audio clip.

    Code (csharp):
    1. var rayLength = 200;
    2.  
    3. function Update(){
    4. if (Input.GetMouseButton(0)) {
    5. var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6. var hit: RaycastHit;
    7.        
    8. if (Physics.Raycast(ray, hit, rayLength)) {
    9. if (!audio.isPlaying) audio.Play();
    10. Invoke("NewLevelFunc", audio.clip.length);
    11. }
    12. }
    13. }
    14.  
    15. function NewLevelFunc() {
    16.     Application.LoadLevel ("Green");
    17. }
    Now it works great!
     
  4. Imari

    Imari

    Joined:
    May 26, 2010
    Posts:
    24
    Argh... I'm having trouble again. I have a scene with four doors (red, blue, yellow, green) and each going to a different scene. Each door is a simple cube mesh with rigidbody and the above script attached. The only thing changed in each script is the name of the destination scene.

    My problem is that when I click on any door, they all lead to the "Green" scene. The doors are in different directions, so it does not seem that I could be getting a hit on the wrong door and not the same green door every time it "misfires." What am I doing wrong, please?