Stuck on this! Please help me out.

I’ve been trying to be implement a breathing system in which walking has its own audio clip, sprinting has its own and after sprinting has a tired breath audio clip.

I managed to get Horizontal and Vertical movement to play the walkBreathing audio but when I attempt to make another if statement for sprinting i try to do if(Input.GetButtonDown(“Sprint”) && Input.GetButtonDown (“Vertical”) || (“Horizontal”). This screws the audio up and over lays the walk audio with the run audio… How can i fix this and make a functioning system here???

var walkBreathing : AudioClip;

var sprintBreathing : AudioClip;

var aftersprintBreathing : AudioClip;

var isWalking : boolean = false;

var isSprinting : boolean = false;

var isAfterSprint : boolean = false;

var isIdle : boolean = false;

 

function Start()
 
{

   
    gameObject.AddComponent(AudioSource);

}

 

function Update ()
{



 

    if(Input.GetButtonDown("Vertical") || Input.GetButtonDown( "Horizontal" ))
	{

    

        audio.clip = walkBreathing;

        audio.Play();

        yield WaitForSeconds (audio.clip.length);
		
		isWalking = true;
	 }	
	 else if(Input.GetButtonUp("Vertical") || Input.GetButtonUp( "Horizontal" ))
	 {	
		
		isWalking = false;
		isSprinting = false;
		isIdle = true;
		is AfterSprinting = false;
 

     }

 

     if(Input.GetButtonDown("Vertical") && Input.GetButtonDown("Sprint"))
     {

        audio.clip = sprintBreathing;                

        audio.Play();

        yield WaitForSeconds (audio.clip.length);
		
		isSprinting = true;
		
		isIdle = false;
		
		isWalking = false;
		
		isAfterSprinting = false;
	
		
	 }
	else if(Input.GetButtonUp("Sprint") && isIdle = true)
	{	
		isSprinting = false;
		
		isAfterSprinting = true;
		
		isWalking = false;
		
		isIdle = true;
		
		audio.clip = aftersprintBreathing;
		
		audio.Play();
		
		yield WaitForSeconds (audio.clip.length);
		
		audio.loop = false;
	
	}

}

Within your existing check for walking also check for sprinting and set the sound accordingly.

if(Input.GetButtonDown("Vertical") || Input.GetButtonDown( "Horizontal" ))
{
    if (Input.GetButton("Sprinting"))
        audio.clip = sprintBreathing;
    else
        audio.clip = walkBreathing;
}

Also, you should implement a flag that keeps the moving state so that you only call audio.Play() when the sound changes. You should have as little as possible going on in your Update() loop. Use helper methods to get the work done for you, and then only call them when needed! :slight_smile: