sounds with key pressed

I’m having issues with the sound aspect of my game. I want the sound to increase/decrease depending on the state of what my character is doing. For example, when I crouch, the footsteps sound decrease, while he’s sprinting the sound increases, etc. Also, I’m having an issue where the sound keeps looping after I’ve released the keys.

I’ve also originally added an else statement, where audio.volume = 0.0f, so that when no key’s are pressed no sound plays. But that caused the volumes to remain at 0.

using UnityEngine;
using System.Collections;

public class FootstepsSound : MonoBehaviour
{
// Use this for initialization
void Start ()
{
audio.volume = 0.0f;
}

// Update is called once per frame
void Update ()
{
//audio.volume ranges from 0 to 1
WalkingSound();
CrouchingSound();
SprintingSound();
}

void WalkingSound()
{
if(Input.GetButtonDown(“Horizontal”) || Input.GetButtonDown(“Vertical”))
{
audio.volume = 0.6f;
audio.Play();
}
}

void CrouchingSound()
{
if(Input.GetButtonDown(“Horizontal”) || Input.GetButtonDown(“Vertical”))
{
if(Input.GetKey(KeyCode.LeftControl))
{
audio.volume = 0.3f;
audio.Play();
}
}
}

void SprintingSound()
{
if(Input.GetButtonDown(“Horizontal”) || Input.GetButtonDown(“Vertical”))
{
if(Input.GetKey(KeyCode.LeftShift))
{
audio.volume = 1.0f;
audio.Play();
}
}
}
}

Please use code tags. I think your update doesn’t wait for the next sound to finish so I don’t know how you would even know. Maybe use a Input.getKey to test each sound.

edit: I see it’s in the functions. I don’t think what you are doing is a good idea. Try using Input.GetKey and a different key for each one, and it would be better to put them in the update and then call the function. Maybe that would work all right, though. It should ignore the others. Anyway, other than the somewhat strange way of calling the functions, it seems like it should work. Use audio.Stop() to stop the sound, but I would use a keydown and keyup or something to test.

Try putting your functions in the update function.

heres what I would recommend:

public float vol = 0;
void Update(){
if(Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical"))
{
audio.volume = vol;
audio.Play();
{
if(Input.GetKey(KeyCode.Z){
 vol = .3f;
}
if(Input.GetKey(KeyCode.X){
vol = .6f;
}
}