- Home /
Sound gets loud in while playing game?
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!
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
This is the same user and question as : http://answers.unity3d.com/questions/346279/when-in-game-sound-is-really-strange-help-please.html#answer-346478
Hey sorry about that, I've got two accounts and I posted the question on this account and then I got on my other account to see the question and I didn't see it, and I thought something had messed up and it hadn't posted it, so I posted it on my other account, and then I realized that I had posted it twice, so sorry about that. Also, you have been a big help with your guide and helping me with this sound, so thanks a lot!
Well ok then, you have to wait for questions and answers to be approved by a moderator before it gets posted. Sometimes people are not free to approve them, e.g. I did 3 pages yesterday, some up to 10 hours old.
If the problem is solved, don't forget to mark the answer as accepted (click on the tick under the thumbs), thanks.
Answer by AlucardJay · Nov 11, 2012 at 03:19 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;
}
Okay, I just tested it and it worked! Again, I'm sorry to everyone about posting this question twice, and thanks $$anonymous$$ $$anonymous$$ay for all the help!!
Your answer
Follow this Question
Related Questions
Trigger Start of Sound 1 Answer
Using stereo sound 1 Answer
Sound effect doesn't play correctly 1 Answer
Can I use SoundCloud music for my game or showcase ? 0 Answers