[Solved]Basic Footsteps

Hi everyone,

I need to create footsteps for a scene and so far have not been able to do so given the following constraints:

  • I’m using the FPS controller (no character model);
  • I need different footsteps sounds as per floor;
  • Footsteps need to take velocity into consideration (more speed == more footsteps).

I have two different scripts for that. One controls which sounds to play (as from the 3rd person example):

SoundEffectControllerFloor.js

var  footAudioSource : AudioSource;
var  footsteps: AudioClip[];

function FootStrike () {
	var volume = Mathf.Clamp01(Time.time);
	footAudioSource.PlayOneShot(footsteps[Random.Range(0, footsteps.Length)], volume);
}

And the other is used to trigger those same sounds:

SoundEffectController.js

function OnTriggerEnter(collision : Collider) {
	var som : SoundEffectControllerFloor = collision.gameObject.GetComponent(SoundEffectControllerFloor);
	som.FootStrike();	
}

The last one is not working, as I get no collisions between my FPS collider and the floor. Any clues?

I also know for a fact that the second code would not do exactly what I need (as in the beggining of the post). Some insights are much appreciated.

Thank you all in advance.

Solved.

Code files in the same order:

var  footsteps: AudioClip[];
var audioStepLength = 0.3;
var footAudio : AudioClip[];

function OnTriggerEnter (other : Collider) {
	var chao : SoundEffectControllerFloor = other.GetComponent(SoundEffectControllerFloor);
	
	if(chao){
		footAudio = chao.footsteps;
	}
}

function PlayStepSounds () {
	var controller : CharacterController = GetComponent(CharacterController);

	while (true) {
		if (controller.isGrounded  controller.velocity.magnitude > 0.3) {
			var volume = Mathf.Clamp01(Time.time);
			audio.PlayOneShot(footAudio[Random.Range(0, footAudio.Length)], volume);
			yield WaitForSeconds(audioStepLength);
		} else {
			yield;
		}
	}
}

function Awake(){
	PlayStepSounds();
}

How to use this script? Can som1 help me with it?

Bump, how to use this script?