I am trying make a footstep sounds when i walk in my First Person Controller. I also would like the footsteps to go faster when i sprint and slower when i crouch. Please help. I’m no coder
Okkkkk … This is a simple example. to a public array (_randomStepSounds)drag a sound samples of random single step. You need audiosource component in the same gameObject as this script.
How it works.
If you press the key ‘w’ or ‘s’ at a given time will be repeated random sound from the array. + Shift ← the sound will be more often played. Try it and let me know if it works, if something goes wrong, we’ll try something else
using UnityEngine;
using System.Collections;
public class Test: MonoBehaviour {
public float walkRepeatingTime=0.8f;
public float runRepeatingTime=0.4f;
private bool isRuning=false;
private bool isWalking=false;
private bool waitForNextStep=false;
private AudioSource _stepSoundPlayer;
public AudioClip[] _randomStepSounds;
void Start()
{
_stepSoundPlayer=gameObject.GetComponent<AudioSource>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S))
{
isWalking=true;
}
if(Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
{
isWalking=false;
}
if(Input.GetKeyDown(KeyCode.LeftShift))
{
isRuning=true;
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
isRuning=false;
}
if(isWalking)
{
if(isRuning && waitForNextStep==false)
{
StartCoroutine(WaitForPlay(runRepeatingTime));
waitForNextStep=true;
}else if(waitForNextStep==false){
StartCoroutine(WaitForPlay(walkRepeatingTime));
waitForNextStep=true;
}
}
}
IEnumerator WaitForPlay(float delayTime)
{
yield return new WaitForSeconds(delayTime);
_stepSoundPlayer.clip=_randomStepSounds[Random.Range(0,_randomStepSounds.Length)];
waitForNextStep=false;
_stepSoundPlayer.Play();
}
}
Okkkkk … This is a simple example. to a public array (_randomStepSounds)drag a sound samples of random single step. You need audiosource component in the same gameObject as this script.
How it works.
If you press the key ‘w’ or ‘s’ at a given time will be repeated random sound from the array. + Shift ← the sound will be more often played. Try it and let me know if it works, if something goes wrong, we’ll try something else
using UnityEngine;
using System.Collections;
public class Test: MonoBehaviour {
public float walkRepeatingTime=0.8f;
public float runRepeatingTime=0.4f;
private bool isRuning=false;
private bool isWalking=false;
private bool waitForNextStep=false;
private AudioSource _stepSoundPlayer;
public AudioClip[] _randomStepSounds;
void Start()
{
_stepSoundPlayer=gameObject.GetComponent<AudioSource>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S))
{
isWalking=true;
}
if(Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
{
isWalking=false;
}
if(Input.GetKeyDown(KeyCode.LeftShift))
{
isRuning=true;
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
isRuning=false;
}
if(isWalking)
{
if(isRuning && waitForNextStep==false)
{
StartCoroutine(WaitForPlay(runRepeatingTime));
waitForNextStep=true;
}else if(waitForNextStep==false){
StartCoroutine(WaitForPlay(walkRepeatingTime));
waitForNextStep=true;
}
}
}
IEnumerator WaitForPlay(float delayTime)
{
yield return new WaitForSeconds(delayTime);
_stepSoundPlayer.clip=_randomStepSounds[Random.Range(0,_randomStepSounds.Length)];
waitForNextStep=false;
_stepSoundPlayer.Play();
}
}