So i’m trying to make a very simple car engine where I press the ‘W’ key and the engine sound gradually increases and when I leave go of the ‘W’ key the volume gradually decreases. I can’t seem to get it to work.
using UnityEngine;
using System.Collections;
public class CarEngine : MonoBehaviour {
float IncVolume = 0.6f;
float DecVolume = 0.6f;
float VolumeRate = 0.1f;
void Update () {
if (Input.GetKeyDown(KeyCode.W))
{
IncVolume = IncVolume + (VolumeRate * Time.deltaTime);
GetComponent<AudioSource>().enabled = !GetComponent<AudioSource>().enabled;
GetComponent<AudioSource>().volume = IncVolume;
}
if (Input.GetKeyUp(KeyCode.W))
{
DecVolume = DecVolume - (VolumeRate * Time.deltaTime);
GetComponent<AudioSource>().volume = DecVolume;
GetComponent<AudioSource>().enabled = !GetComponent<AudioSource>().enabled;
}
}
}
I have the IncVolume and DecVolume at 0.6 because that is the volume thats in the Inspecter and I want to increase and decrease the volume by 0.1 every second or so.