Hey Guys, quick one. I want this walking audio to play continuously if 1 or more keys (w, a, s, d) is being held down without it stopping.
lets say you hold down W (Audio Plays), and then also hold D (Audio is still playing) but then let go of D so only W is being held down but I want the audio to carry on until all keys are lifted.
How can i get it so it continues the audio until all keys are lifted?
public class TwoDimensionalStateController : MonoBehaviour
{
Animator animator;
public float isMoving = 0f;
public AudioSource WalkingSource;
public AudioClip WalkingClip;
void Start()
{
WalkingSource = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W) || (Input.GetKeyDown(KeyCode.S) || (Input.GetKeyDown(KeyCode.A) || (Input.GetKeyDown(KeyCode.D)))))
{
isMoving = 1;
if (isMoving == 1)
{
WalkingSource.PlayOneShot(WalkingClip);
}
}
else if (Input.GetKeyUp(KeyCode.W) || (Input.GetKeyUp(KeyCode.S) || (Input.GetKeyUp(KeyCode.A) || (Input.GetKeyUp(KeyCode.D)))))
{
isMoving = 0;
if (isMoving == 0)
{
WalkingSource.Stop();
}
}
So with this im guessing id have to do the code for each key on a new line then reference if the other keys are being pressed so they dont overlap each other if that makes sense?
It may sound a little strange if you abruptly stop the audio mid footstep. Instead you can play a sound of a single footstep and then you don’t need to worry about stopping it. You also can just check the character’s velocity instead of all the keys.
It shouldn’t do. But either way you don’t need to add an extra check to the code there. The ‘isMoving’ is being set to one, then being instantly checked by the code to see if it is one. It will never not be one in that situation.
Just realised the reason it’s stopping for you is probably that you unticked ‘looping’ on the Audio Source.
Of course, this method may result in half of one step being played, then it being finished the next time you move. It’s up to you if this is distracting or not. If it is, I can talk you through a more complex method involving the single step approach - which is superior and can accomadate multiple surfaces.