How to play a sound while walking?

I know this sounds stupid but I am still going to try to get some help.
I have tried to play a sound while my character moves (he can walk and sprint),but I can play the sound only with D key… :face_with_spiral_eyes:
What I’m trying to do is,to play the sound while pressing W,A,S,D and while I’m running (with shift).
Here is my code which I hope means something.It is in javascript,for moving and playing the sound.

var AudioFile : AudioClip;
function Update() {
 
if (Input.GetKeyDown (KeyCode.W))
{
    audio.clip = AudioFile;
    audio.Play();
    }

if (Input.GetKeyUp (KeyCode.W))
{
    audio.clip = AudioFile;
    audio.Pause();
    }
    
if (Input.GetKeyDown (KeyCode.A))
{
    audio.clip = AudioFile;
    audio.Play();
    }

if (Input.GetKeyUp (KeyCode.A))
{
    audio.clip = AudioFile;
    audio.Pause();
    }
    
if (Input.GetKeyDown (KeyCode.S))
{
    audio.clip = AudioFile;
    audio.Play();
    }

if (Input.GetKeyUp (KeyCode.S))
{
    audio.clip = AudioFile;
    audio.Pause();
    }

if (Input.GetKeyDown (KeyCode.D))
{
    audio.clip = AudioFile;
    audio.Play();
    }

if (Input.GetKeyUp (KeyCode.D))
{
    audio.clip = AudioFile;
    audio.Pause();
    }
    
if (Input.GetKeyDown ("left shift"))
{
    audio.clip = AudioFile;
    audio.pitch = 2;
    }
if (Input.GetKeyUp ("left shift"))
{
    audio.clip = AudioFile;
    audio.Pause ();
    }
 
if (Input.GetKeyDown ("right shift"))
{
    audio.clip = AudioFile;
    audio.pitch = 2;
    }
if (Input.GetKeyUp ("right shift"))
{
    audio.clip = AudioFile;
    audio.Pause ();
    }

if(Input.GetKey(KeyCode.W)  !Input.GetKey(KeyCode.LeftShift)){
       audio.clip = AudioFile;
       if(!audio.isPlaying)
         audio.Play();
         audio.pitch = 1;
    }
    else if(Input.GetKey(KeyCode.W)  Input.GetKey(KeyCode.LeftShift)){
       audio.pitch = 2;
       if(!audio.isPlaying)
         audio.Play();
    }
       
if(Input.GetKey(KeyCode.A)  !Input.GetKey(KeyCode.LeftShift)){
       audio.clip = AudioFile;
       if(!audio.isPlaying)
         audio.Play();
         audio.pitch = 1;
    }
    else if(Input.GetKey(KeyCode.A)  Input.GetKey(KeyCode.LeftShift)){
       audio.pitch = 2;
       if(!audio.isPlaying)
         audio.Play();
    }
       
if(Input.GetKey(KeyCode.S)  !Input.GetKey(KeyCode.LeftShift)){
       audio.clip = AudioFile;
       if(!audio.isPlaying)
         audio.Play();
         audio.pitch = 1;
    }
    else if(Input.GetKey(KeyCode.S)  Input.GetKey(KeyCode.LeftShift)){
       audio.pitch = 2;
       if(!audio.isPlaying)
         audio.Play();
    }
       
if(Input.GetKey(KeyCode.D)  !Input.GetKey(KeyCode.LeftShift)){
       audio.clip = AudioFile;
       if(!audio.isPlaying)
         audio.Play();
         audio.pitch = 1;
    }
    else if(Input.GetKey(KeyCode.D)  Input.GetKey(KeyCode.LeftShift)){
       audio.pitch = 2;
       if(!audio.isPlaying)
         audio.Play();
    }
    else
       audio.Pause();
}

I am sorry if I haven’t done/said something like I should.This is my first thread…If it means something. :sweat_smile:

Hmmm. That’s a lot of if statements.

Perhaps try do something like this. It’s easier to manage and expand. Sorry, my example code is in Boo but it’s basicly the same but with few syntax differences in Unity/JavaScript.

import UnityEngine

class SoundManager (MonoBehaviour): 
	
	public WalkClip as AudioClip 
	
	public walkPitch as single = 1
	public runPitch as single = 2
	
	private final _walkKeyList = (KeyCode.W, KeyCode.S, KeyCode.D, KeyCode.A)
	private final _runKeyList = (KeyCode.LeftShift, KeyCode.RightShift)

	def Audio(file as AudioClip):
		
		if not file:
			 audio.Pause()
		
		elif not audio.isPlaying:
			audio.clip = file
			audio.Play()
			
	def Audio(pitch as single):

		audio.pitch = pitch
		
	def GetKeysDown(keys as (KeyCode)) as bool:

		for key as KeyCode in keys:
			if Input.GetKeyDown(key): return true
		return false
		
	def GetKeys(keys as (KeyCode)) as bool:
		
		for key as KeyCode in keys:
			if Input.GetKey(key): return true
		return false
		
	def Update():
		
		if GetKeysDown(_walkKeyList): Audio(WalkClip)
		elif not GetKeys(_walkKeyList): Audio(null)
			
		if GetKeys(_runKeyList): Audio(runPitch)
		else: Audio(walkPitch)

Well,I would be really happy if I could get the code from C#/Java because it means a lot to me.
I use them both and if I use Boo here,I’ll have to rewrite some code in the game but thank You for helping me.

This is something that could be added to your motion script. As far as it only playing on the “d” key. I have no idea why that would be. It’s the same code as the rest.

otherwise:

if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
{
//play sound
}

else {
//pause sound
}

It’s fixed.I changed “D” key with “F” but when I walk/run it doesn’t matter my character walks/runs with WASD not WASF.Thanks

Your code also helped me! ^^

Here is my solution, i use one method which checks the grounded bool and if you are moving. It then sets the pitch depending on the current Movementstate. I have a state machine which is called on update just like the soundControl method. The state is set to sprint state if the left shift key is pressed, otherwise the walking state is set. In my project the audio clip is not on the object which my script is on, in your case you can just use audioClip.pitch = 1f;

private void SoundControl()
    {
        if (grounded && Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) // if you are moving and on the ground
        {
            // check the state to set the pitch
            if (state == MovementState.sprinting)
            {
                am.sounds[1].source.pitch = 1.5f;
            }
            if(state == MovementState.walking)
            {
                am.sounds[1].source.pitch = 1f;
            }
            if (am.sounds[1].source.isPlaying) return; // checks if the sound is already playing, if so then return
            am.Play("Walk");
        }
        else am.Stop("Walk");
    }