- Home /
[Help] How to prevent sound repeating (in this script)
The premise of the below script is; the object it's attached to should move towards the player when their back is turned, and not move when it is being looked at. While it is moving however, it should make a noise which is declared by the movingSound variable.
Since this is all done via the update function, I've created a Boolean for when the sound should/shouldn't be played; to stop the sound from playing every frame. Despite my efforts, it still seems to play over each-other; the sound echoing over itself in-game, and I can not for the life of me figure out why. Any help would be greatly appreciated.
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var stopSpeed = 0;
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
var movingSound : AudioClip;
public var soundplaying : boolean;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
soundplaying = false; //Stop the sound from playing on start.
}
function Update () {
var Speed = moveSpeed;
if (renderer.isVisible)
{
myTransform.position += myTransform.forward * stopSpeed * Time.deltaTime;
rigidbody.isKinematic = true;
soundplaying = false;
}
if(!renderer.isVisible)
{
rigidbody.isKinematic = false;
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
soundplaying = true;
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
if (soundplaying == true) {
audio.PlayOneShot(movingSound, 1);
}
if (soundplaying == false) {
audio.Stop();
}
}
Answer by Owen-Reynolds · Oct 06, 2013 at 08:26 PM
Having a sound play, then not be able to play again for 2 seconds -- that's that same math as being able to shoot a bullet only once every two seconds. Should be able to look up limiting bullets/jumps/... and copy it.
Just in case, the Unity Way of making a delay looks like this. It goes inside all the other "do I want to play the sound" logic:
float nextSoundTime=0; // can't play sounds if before this time
// can't play if last one not finished:
if(Time.time>=nextSoundTime) {
audio.PlayOneShot(someSound);
// write down when we will be finished:
nextSoundTime = Time.time + someSound.length;
}
The problem with this is; let's say the sound clip is ten seconds long. So, that's a ten second delay before the next sound can be played- to prevent the sounds from overlapping. But, there's nothing to say the player won't look at the object before that ten seconds is up. The player looks at it, then looks away, and the object is now moving without the sound playing as the ten seconds from the last one isn't up yet.
This is a good temporary solution though, thanks a ton for trying to help.
Accepting this answer as correct.
To fix the issue I posted above, I made sure the sound was meant to be playing AND it was after the previous sound ended via your script. Then I made it so when the person looks at the entity, the sound not only stops, but the nextSoundTime counter is set to zero so the following sound could play when it was needed.
Thanks a ton for your help, and everyone else's.
Answer by clunk47 · Oct 06, 2013 at 08:45 PM
Just check if the audio is already playing or not.
if(!audio.isPlaying)
audio.Play();
I'm sorry, I don't quite see how you'd like this function to be implemented to where it'd be helpful with the current issue. If you could explain further, that'd be great. If not, thanks for posting anyways.
Just implement it into your current code ins$$anonymous$$d of using an extra boolean. Read the documentation. audio.isPlaying is a bool that checks if your sound is playing or not. If it IS playing, this keeps it from playing again. It will only play your audio clip if it is not currently playing... That's the only way to really explain it lol. I don't have the time to completely rewrite your script, I'm just giving you an example and a link to the documentation on how to use it.
Answer by meat5000 · Oct 06, 2013 at 07:31 PM
How about this?
if (soundplaying == true)
{
audio.PlayOneShot(movingSound, 1);
soundplaying = false;
}
Then make the stop routine an else or leave it out?
If I delete the stop routine the sound plays over itself still, regardless of rather or not the object is visible.
When I turn the stop routine into an else statement with that code block you've made, it has the same result as before; the audio continues to play every frame over itself. Thanks for trying to help though.
Your answer
Follow this Question
Related Questions
Sound Problem ! 1 Answer
How to play sound when object stops moving? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Checking if object intersects? 1 Answer