- Home /
How to make volume continually go down in C#Script ?
Hello everyone !
I've been searching on this website for a few days now and haven't quite found what I was looking for, so I hope one of you will be able to save me !
Here's what I'm looking for, I have an audioclip inside a script, playing music for my main scene. What I want to do is to make the music fade out all the time because in my game, when the volume of the music gets to zero, it's Game Over. But the player can raise the volume up by collecting some sort of "power-up" objects, and the music fades out again until he finds another object, and so on until the player finishes the level.
I hope I've been clear enough, here's what my code looks like for now, it's attached to my player, thank you !
using UnityEngine;
using UnityEngine.Audio;
using System.Collections;
public class Audio : MonoBehaviour {
public AudioSource Mainaudio;
public float volume = 0f;
// Use this for initialization
void Start () {
Mainaudio.Play ();
}
void OnTriggerEnter2D(Collider2D other){
if (other.gameObject.tag == "Power Up") {
}
}
void Update () {
Mainaudio.volume = 0.5f;
}
}
Answer by TwinPrime · May 24, 2015 at 02:49 PM
IEnumerator goDownforWhat ()
{
yield return new WaitForSeconds (0.3f);
mainaudio.volume -= 0.5f;
}
Please, mark this as the correct answer so we know it requires no more attention, thanks :)
Answer by venhip · May 24, 2015 at 11:33 PM
Just for anyone else coming across this later, another solution would be as follows:
void Update(){
Mainaudio.Volume -= Time.deltaTime * speedMultiplier;
}
This would make the volume decrease by 1 for each second that passes, if speedMultiplier == 1. If speedMultiplier == 2, then for each second that went past it would reduce by 2 volume points, etc.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Need help with my slider 3 Answers
Multiple Cars not working 1 Answer
Music play throughout all scenes? 1 Answer
Next song problem (music player) 1 Answer