- Home /
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....
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.
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?
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");
}
}
}
}
$$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
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");
}
}
}
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.
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.
ill try, haha.. so inside the first if(Physics.Raycast.... outside the collision detection set it back to true?
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!