Footsteps on different surfaces

Hey guy’s,

I was wondering how I can make footsteps when the player walks on different surfaces im guessing I should use a Tag like Woodfloor and when the player is in contact it would trigger the footsteps looping like a player is walking… But I was wondering I dont quite understand it well can someone maybe show me a example of it and I can add to it? or explain it?

You can do a Raycast from the player to the floor, it would return the info of what it hit and like you said, you can determine the type of floor it is by its tag.

for example:

void Update () 
{
	RaycastHit hit;
	if (Physics.Raycast(transform.position, Vector3.down, out hit))
	{
		//This is the colliders tag
		floorTag = hit.collider.tag;
	}
	
	//Then you use the floorTag to choose the type of footstep
	if (floorTag == "woodenFloor")
	{
		//do wood
	}
	if (floorTag == "stoneFloor")
	{
		//do stone
	}
}

Heres the unity reference for Raycasts http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

hope that helped

Sorry for the late reply, but I was able to get my script working by replacing:

function OnCollisionEnter(collision : Collision){
      var controller : CharacterController = GetComponent(CharacterController);

with:

function OnControllerColliderHit (hit : ControllerColliderHit){

Here is the example of the full script to put it into context:

var stepWood:AudioClip[];
var stepMetal:AudioClip[]; // fill this array with the sounds at the Inspector
var stepConcrete:AudioClip[];

var footSource1 : AudioSource;
var footSource2 : AudioSource;

var delayBeforeSteps : float = 0.20;
var delayBetweenSteps: float = 0.45;
var audioStepLength : float = 0.60;
var groundType : int;
var isPlayerWalking : float = 0.0;

var footAudioRandomness = 0.1;
var soundEffectPitchRandomness = 0.05;


function Update(){
    //Check to see if the player is walking by checking for input from the walking keys
    if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
       //if those keys are pressed, the player must be walking...
       isPlayerWalking += Time.deltaTime;
    }
    else{
       //if those keys aren't pressed, the player can't be walking.....
       isPlayerWalking = 0;
    }
}

function Start(){


var aSources = GetComponents(AudioSource);
    footSource1 = aSources[0];
    footSource2 = aSources[1];

       while(true){
         if (isPlayerWalking >= 0.20 && groundType == 1){
         	yield WaitForSeconds(delayBeforeSteps);
			footSource1.clip = stepMetal[Random.Range(0, stepMetal.length)];
    		footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    		footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        	footSource1.Play();
        		if(isPlayerWalking == 0){
        			yield;}
        	else{
            yield WaitForSeconds(delayBetweenSteps);
            footSource2.clip = stepMetal[Random.Range(0, stepMetal.length)];
    		footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    		footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        	footSource2.Play();
        	yield WaitForSeconds(audioStepLength);
        		if(isPlayerWalking == 0){
        			yield;}
        		}
            
    }
          else if ( isPlayerWalking >= 0.20 && groundType == 2){
          	yield WaitForSeconds(delayBeforeSteps);
        	footSource1.clip = stepWood[Random.Range(0, stepWood.length)];
        	footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    		footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        	footSource1.Play();
        		if(isPlayerWalking == 0){
        			yield;}
        	else{
            yield WaitForSeconds(delayBetweenSteps);
            footSource2.clip = stepWood[Random.Range(0, stepWood.length)];
        	footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    		footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        	footSource2.Play();
            yield WaitForSeconds(audioStepLength);
            	if(isPlayerWalking == 0){
            		yield;}
            	}
   }
   		else if ( isPlayerWalking >= 0.20 && groundType == 3){
          	yield WaitForSeconds(delayBeforeSteps);
        	footSource1.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
        	footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    		footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        	footSource1.Play();
        		if(isPlayerWalking == 0){
        			yield;}
        	else{
            yield WaitForSeconds(delayBetweenSteps);
            footSource2.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
        	footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    		footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        	footSource2.Play();
            yield WaitForSeconds(audioStepLength);
            	if(isPlayerWalking == 0){
            		yield;}
            	}
   }
   
        else{
          yield;
        }
    }
}


function OnControllerColliderHit (hit : ControllerColliderHit){
    if (hit.gameObject.tag == "metalFloor"){
      groundType = 1;
      print("Metal");
    }
     else if (hit.gameObject.tag == "woodFloor"){
       groundType = 2;
       print("Wood");
  	}
  	 else if (hit.gameObject.tag == "concreteFloor"){
       groundType = 3;
       print("Concrete");
  }
}

Now this is a valid way to do it, but I ended up syncing the audio to the animation of the character by adding events in the animation editor, so the script I ended up using is actually a little bit more simplified, but still derived from this.

Hope this is helpful!

Ok guys,

This is something that I thought of recently as well…

So, because you guys were asking about this, I thought I would try and write a little script from scratch… Now I haven’t tested this, so there may be some errors, but this is basically how I would do it…:

//Written by Grady Featherstone
//Create a variable to tell whether the player is walking or not
var isPlayerWalking = false;
var metalFloorSound : AudioClip;
var grassDirtGroundSound : AudioClip;
var riverStreamPuddlesSound : AudioClip;

function Update(){
	//Check to see if the player is walking by checking for input from the walking keys
	if(Input.GetKeyDown("a") || Input.GetKeyDown("w") || Input.GetKeyDown("s") || Input.GetKeyDown("d")){
		//if those keys are pressed, the player must be walking...
		isPlayerWalking = true;
	}
	else{
		//if those keys aren't presesd, the player can't be walking.....
		isPlayerWalking = false;
	}
}

function OnCollisionEnter(collision : Collision){
	//Check if the player is walking...
	if(isPlayerWalking == true){
		//If the player is walking, check what sort of ground they're walking on, and then play that sound...
		if(collision.gameObject.tag == "metalFloor"){
			audio.Play(metalFloorSound);
		}
		else if(collision.gameObject.tag == "grassDirtGround"){
			audio.Play(grassDirtGroundSound);
		}
		else if(collision.gameObject.tag == "riverStreamPuddles"){
			audio.Play(riverStreamPuddlesSound);
		}
	}
}

You would have to tag each different type of ground something different, as you can see in the script, and then add this script to your player, and it would basically check to see if the player is moving, and if so, what surface they are moving on, and would then play the footsteps sound for that particular ground!

I hope this works, but as I say, I just quickly wrote it then, so no guarantees, but if anyone is interested, then I can try and seriously see if I could get this working nicely!

-Grady

Or get this asset: Advanced Footstep System | Audio | Unity Asset Store