How to make different walk sounds

Hello there! I have a script where you can walk and run, and it will play a sound. But, I need a script where different surfaces or objects when walking upon them, different sounds begin. I dont want it to be said to me that you can just put different sounds next to the object. Here is my script:

#pragma strict
@script RequireComponent( AudioSource )
 
var walk : AudioClip;
var run : AudioClip;
 
var walkAudioSpeed : float = 0.4;
var runAudioSpeed : float = 0.2;
 
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
 
var isWalking : boolean = false;
var isRunning : boolean = false;
 
var walkSpeed: float = 7; // regular speed
var runSpeed: float = 20; // run speed
 
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
 
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
 
function Update()
{
    SetSpeed();
 
    if ( chCtrl.isGrounded )
    {
       PlayFootsteps();
    }
    else
    {
       walkAudioTimer = 0.0;
       runAudioTimer = 0.0;
    }
}
 
function SetSpeed()
{
    var speed = walkSpeed;
 
    if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
    {
       speed = runSpeed;
    }
 
    chMotor.movement.maxForwardSpeed = speed;
}
 
function PlayFootsteps()
{
    if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
    {
       if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
       {
         // Running
         isWalking = false;
         isRunning = true;
       }
       else
       {
         // Walking
         isWalking = true;
         isRunning = false;
       }
    }
    else
    {
       // Stopped
       isWalking = false;
       isRunning = false;
    }
 
    // Play Audio
    if ( isWalking )
    {
       if ( audio.clip != walk )
       {
         audio.Stop();
         audio.clip = walk;
       }
 
       //if ( !audio.isPlaying )
       if ( walkAudioTimer > walkAudioSpeed )
       {
         audio.Stop();
         audio.Play();
         walkAudioTimer = 0.0;
       }
    }
    else if ( isRunning )
    {
       if ( audio.clip != run )
       {
         audio.Stop();
         audio.clip = run;
       }
 
       //if ( !audio.isPlaying )
       if ( runAudioTimer > runAudioSpeed )
       {
         audio.Stop();
         audio.Play();
         runAudioTimer = 0.0;
       }
    }
    else
    {
       audio.Stop();
    }
 
    // increment timers
    walkAudioTimer += Time.deltaTime;
    runAudioTimer += Time.deltaTime;
}

You could use physic materials on the floors and play a sound based on the physic material that is being walked on. I did this to mine.

How do I do that?

Well, you just do a check in OnCollisionStay for what physic material the player is standing on, and play the appropriate sound. You could by all means just use tag's for each floor surface.

@monstergaminghd, which part of these methods did you not understand? They are properly explained, and you can find lots of examples using Google with keywords like "check physic material", "gameObject tag", "trigger", "assign variable in another script"

2 Answers

2

OK, based on your code, i came up with a quick 2 minute solution, it should work but not guaranteed because i don’t use UnityScript, but here goes.

Simply tag the different floor surfaces, i.e ConcreteFloor, WoodFloor, etc. Then you can easily add more types of floors in.

#pragma strict
@script RequireComponent( AudioSource )
 
var defaultFloorWalk : AudioClip;
var defaultFloorRun : AudioClip;

var concreteFloorWalk : AudioClip;
var concreteFloorRun : AudioClip;
 
var woodFloorWalk : AudioClip;
var woodFloorRun : AudioClip;
 
var walkAudioSpeed : float = 0.4;
var runAudioSpeed : float = 0.2;
 
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
 
var isWalking : boolean = false;
var isRunning : boolean = false;
 
var walkSpeed: float = 7; // regular speed
var runSpeed: float = 20; // run speed
 
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
 
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
 
function Update()
{
    if ( chCtrl.isGrounded )
    {
		if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
		{
			if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
		   {
			 // Running
				isWalking = false;
				isRunning = true;
				chMotor.movement.maxForwardSpeed = runSpeed;
			}
			else
			{
				// Walking
				isWalking = true;
				isRunning = false;
				chMotor.movement.maxForwardSpeed = walkSpeed;
			}
		}
		else
		{
			// Stopped
			isWalking = false;
			isRunning = false;
		}
		// increment timers
		walkAudioTimer += Time.deltaTime;
		runAudioTimer += Time.deltaTime;
    }
    else
    {
       walkAudioTimer = 0.0;
       runAudioTimer = 0.0;
    }
}

function OnCollisionEnter(collisionInfo : Collision){
	 PlayFootstepSound(collisionInfo.gameObject.tag);
}

function PlayWalkingSound(clip : AudioClip){
	if (audio.clip != clip){
		audio.Stop();
		audio.clip = clip;
	}
	if ( walkAudioTimer > walkAudioSpeed )
       {
         audio.Stop();
         audio.Play();
         walkAudioTimer = 0.0;
       }
}

function PlayRunningSound(clip : AudioClip){
  if ( audio.clip != clip )
       {
         audio.Stop();
         audio.clip = clip;
       }
 
       if ( runAudioTimer > runAudioSpeed )
       {
         audio.Stop();
         audio.Play();
         runAudioTimer = 0.0;
       }
}

function PlayFootstepSound(floortype : String)
{	
	switch (floortype){
		case "ConcreteFloor":
			if (isWalking)
				PlayWalkingSound(concreteFloorWalk);
			else
				PlayRunningSound(concreteFloorRun);
			break;
		case "WoodFloor":
			if (isWalking)
				PlayWalkingSound(woodFloorWalk);
			else
				PlayRunningSound(woodFloorRun);
			break;
		default:
			if (isWalking)
				PlayWalkingSound(defaultFloorWalk);
			else
				PlayRunningSound(defaultFloorRun);
			break;
	}
}

You can modify the switch statement to add more types of floor, and tag the floor appropriatly.

This is the best i could come up with in the few minutes spare time i had.

Make a guide tutorial on it. I don’t really understand . I understand word better

You should not respond with an Answer if it is not an Answer.

Its the same as BoredMormon did but it checks first. The DontDestroy does not contribute to the effect.