- Home /
Stop audio from looping and play at lower volume
When my player dies I stop the game music that's attached to a game object with an audio source. I then play the "game over" music which is activated through the audio source on my player. I'm just starting out w/ audio in unity so I'm hoping this is just an easy fix. Currently none of my audio commands (audio.volume, audio.loop, audio.panLevel) work... and i'm not sure why. I just want the "game over" music to play one time at a lower volume level. currently it's playing at max volume and keeps looping. Any help to fix this is appreciated. Thanks!
if (currentLife == 0){
GetComponent(EnemyDestroyPlayer).enabled = false;
Time.timeScale = 0.0001;
gameMusic.active = false;
if(!audio.isPlaying) {
audio.clip = playerDeath;
audio.Play();
audio.loop = false;
audio.volume = 0.1;
audio.panLevel = 0.0;
}
player.animation["Die"].wrapMode = WrapMode.ClampForever;
player.animation.Play("Die");
player.animation["Die"].speed = 1.0/Time.timeScale;
yield WaitForSeconds(5.0 * Time.timeScale);
Camera.main.GetComponent(GameManager).SetGameOver();
}
Answer by rutter · May 17, 2012 at 06:15 AM
audio
gives you the AudioSource attached to the current GameObject (script reference).
`AudioSource.PlayClipAtPoint()` is a static function, and I think creates a brand new AudioSource for the playback. Handy in a pinch if you just want to play a clip, but not as much if you need to control it past that. The script reference page for that function includes a bit of example code which can give you that control.
So you are adjusting the volume of an AudioSource -- probably not the right one, though. ;)
As far as the looping, you might be able to set that in the clip. I know you can set it on an AudioSource, in the inspector or at runtime, by toggling its loop flag.
ya I checked all the docs out... just played around with it some more. What was happening was countless oneshotaudio's were being spawned resulting in an echoing loop at max volume. i fixed this by changing up the code. And the volume now works and just one audio is being played... however it is STILL looping haha... one step in the right direction though. I edited my code above... this is what it looks like now... just trying to get it to stop looping now
Solved the looping issue. It was looping because it was being called in the update function if the player's life was = to 0. So after the loop finished I just disabled the script and no more looping