- Home /
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!
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
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;
}
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. :)
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
Follow this Question
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