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 zTizzle · Nov 11, 2012 at 04:45 AM · javascriptsoundjava

When in game, sound is really strange. Help Please!?

Hi, I'm making a Slender based game, and I am adding a sound so that when you get the first page it plays some background music. Here is the code that I am using to play the sound (javascript):

pragma strict

 var papers : int = 0;
 var papersToWin : int = 8;
 var distanceToPaper : float = 2.5;
 public var paperpickup : AudioClip;
 public var onepage : AudioClip;
 
 function Start() 
 {
     Screen.lockCursor = true;
 }
 
 function Update() 
 { 
     if ( Input.GetMouseButtonUp(0) )
     {
         var ray = Camera.main.ScreenPointToRay( Input.mousePosition );
         var hit : RaycastHit;
         if ( Physics.Raycast( ray, hit, distanceToPaper ) )
         {
             if ( hit.collider.gameObject.tag == "Paper" )
             {
                 papers += 1;
                 audio.PlayOneShot(paperpickup);
                 Debug.Log( "A paper was picked up. Total papers = " + papers );
                 Destroy( hit.collider.gameObject );
             }
         }
     }
 
     if ( papers == 1 )
     {
         audio.PlayOneShot(onepage);
     }
 
 }
 
 function OnGUI()
 {
     if ( papers < papersToWin )
     {
        GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "" + papers + " Papers" );
     }
     else
     {
        GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
     }
 }

The part for the music to be played is this part:

     if ( papers == 1 )
     {
         audio.PlayOneShot(onepage);
     }

When I play the game, however, it sounds really weird and strange. Here is what I mean: http://youtu.be/Ti6KfnOk8O8 Also, when I preview the sound that I am using, it sounds normal, as you can see in the video, but once I play the scene, it sounds weird. Can anyone help me with this? Any help is appreciated. Thanks!

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 AlucardJay · Nov 11, 2012 at 03:08 PM 1
Share

This is the same question as : http://answers.unity3d.com/questions/346171/sound-gets-loud-in-while-playing-game.html

Did you create another account just to ask the same question? That is really Not cool. I am still deciding whether to report this or not. If you want to improve your karma, just ask better questions (or don't just ask for scripts). I was really happy to see my guide being used when I saw the video, but now ...

Anyway, here's a copy of my response for all future readers :

That is really strange! I was going to suggest use 2D sounds, but you have. Does the same thing happen when you test with a different audioClip?

I did actually make a change to that script, the audio line is now :

audio.PlayClipAtPoint( paperPickup, transform.position );

try that also and see if it fixes the problem (if it does, I'll move this to an answer!)

I just uploaded a new version of the guide , the link is on my answer here : http://answers.unity3d.com/questions/321749/how-do-you-pick-up-pages-like-in-slender.html

2 Replies

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

Answer by AlucardJay · Nov 11, 2012 at 03:15 PM

The actual problem here is where you put

 if ( papers == 1 )
 {
     audio.PlayOneShot(onepage);
 }

This is in Update, therefore you are making a sound every frame. to do what you are doing, you need to set up a boolean, so when the papers == 1, play the sound once, then change the boolean so it doesnt play again. Have a look at the footstep script I wrote in V2 .

 var playedOnePaperSound : boolean = false;
 
 // UPDATE
 if ( papers == 1 && !playedOnePaperSound  )
 {
     audio.PlayOneShot(onepage);
     playedOnePaperSound = true;
 }
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 YoungRichi · Apr 20, 2018 at 02:49 AM 0
Share

Thank you. It worked!!

avatar image
0

Answer by Lord Darkshayde · Nov 11, 2012 at 02:55 PM

I might be wrong about this, but have you tried checking the box to make it a 3D sound? That could be what is causing the distortion you are receiving in your drumbeat WAV. Hope this helps. :)

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 AlucardJay · Nov 11, 2012 at 03:13 PM 0
Share

The way the scripts are set up , it is better to use 2D (I wrote the scripts : http://answers.unity3d.com/questions/321749/how-do-you-pick-up-pages-like-in-slender.html)

Unless the OP used the information from the raycast hit (when checking the player) and again used audio.PlayClipAtPoint( enemySightedSound, hit.point );

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Play and stop sound 1 Answer

Ladder script not working? 0 Answers

Footsteps Java Script Problems 0 Answers

I want my trigger sound only to play once! 0 Answers


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