How To Add FootSteps Sound Effect When Walking?

I Am currently testing out my indie game I’m developing, but i have a problem. I can get my FootSteps Sound Effect To Work For Some Reason. Here Is The Script:

var AudioFile : AudioClip;
function Update() {

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

 
}
}

Is It The Script That I Put in the first person controller, or did i just do EVERYTHING wrong, btw the sound effect is a .mp3 if thats helpful to know.

Your code will play the audio just once when you press W; it is not a desired effect for footstep sound effect. Take a look at these 2 scripts written by @alucardj, [Footstep audio script with delay][1] and [Simple Footstep audio script][2]. Run the script, understand the logic, and make the necessary modifications to make it work with your game. [1]: http://answers.unity3d.com/questions/373508/footsteps-script-for-running-and-walking.html [2]: http://answers.unity3d.com/questions/343383/switch-between-two-different-sounds-for-walking-an.html

–

What was wrong before this?

–

2 Answers

2

Your logic will play the sound once when you pressed W. It will play the sound.

Make sure you have these in your scene setup :

  • Audio Listener on the camera (this is the default)
  • The audio component set to a audible volume
  • the audiolistener placed within the audible zone
  • audio-file assigned to the script

.... and the gameObject that is going to play must have an audio source component attached (as the OP discovered).

–

var ground : AudioClip;

private var step : boolean = true;
var audioStepLengthWalk : float = 0.45;

function OnControllerColliderHit (hit : ControllerColliderHit) {
var controller : CharacterController = GetComponent(CharacterController);

if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == “Ground” && step == true ||
controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == “Untagged” && step == true ) {
WalkOnGround();
}
}
/////////////////////////////////// Ground ////////////////////////////////////////
function WalkOnGround() {
step = false;
audio.clip = ground[Random.Range(0, ground.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
@script RequireComponent(AudioSource)

my code is not working please help it out