Think o f this code as engine noise. when I press W (gas peddle) to move, the engine sound fades up to a rev, when I let up on W, I want the engine sound to fade down. I have working scripts that fade in and out onTriggerEnter and onTriggerExit and this is basically that with keypress substituted for entering trigger zones
using UnityEngine;
using System.Collections;
public class waterFading : MonoBehaviour {
AudioSource source;
public float speedIn = .4f;
public float speedOut = .1f;
public float maxV = .5f;
public float minV = .05f;
public bool moving;
void Awake() {
source = GetComponent<AudioSource>();
}
void Start(){
moving = false;
}
void Update(){
if(moving == false)
{
if(source.volume > minV)
{
source.volume -= Time.deltaTime * speedOut;
}
}
if (Input.GetKey(KeyCode.W))
{
moving = true;
if(source.volume < maxV)
source.volume += Time.deltaTime * speedIn;
}
if (Input.GetKeyUp(KeyCode.W))
{
moving = false;
}
}
}