- Home /
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;
}
}
Answer by iwaldrop · Mar 30, 2013 at 12:33 AM
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! :)
Or, since you may be using the isSprinting flag for other things, just do this first:
isSprinting = Input.GetButton("Sprinting");
Then check isSprinting for everything that you need.
Another note:
Ideally you have an InputController that gets your input and makes references for other scripts to use.
Take your audio, for instance. You don't want to check Input.GetButton() for every script that needs to know about it, because that takes longer. Your InputController script will check Input.GetButton() and do clever things with it, like set a Vector2 to store the value of the axis, and set flags like isSprinting and is$$anonymous$$oving for scripts like your AudioController to use.
Then you AudioController just needs a reference to your InputController and check inputController.isSprinting to change between the walking and running clips. At the same time your $$anonymous$$ovementController can check InputController.movementVector for it's input values!
If you want to get super fancy, use delegates and events to let the AudioController and $$anonymous$$ovementController know when they should act.
Note: InputController, AudioController, and $$anonymous$$ovementController are just example names; you may wish to call them something else.
so like this would work?
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" ))
{
if (Input.GetButtonDown("Sprint"))
audio.clip = sprintBreathing;
else
audio.clip = walkBreathing;
}
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;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Change Music When GameObject is Grabbed? 1 Answer
Can anyone help me invert this simple script? 2 Answers
Trying to simply play this animation... Help please. 1 Answer
Native visual scripting? 0 Answers