- Home /
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 $$anonymous$$e.
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.
Hello? This is my script remember? I have a script ... don't forget to give credit where credit is due. People will appreciate that you recognize their efforts, and decide to help you further if you actually show some gratitude.
What you are asking is quite technical, AAA title stuff.
Elite$$anonymous$$ossy has actually given you the 2 methods to accomplish this.
Use a trigger volume, and on entering that trigger swap to a different set of sounds.
Or read the information from the texture under which the character is standing, then use that information to choose what sound set you are playing from.
That is how you do it. What you are asking is for someone to write it for you, perhaps you should have done it on my forum page, (considering it's my script and all ...., I know exactly what it's doing)
Finally to put a positive spin on things, I will give away another method of at least randomizing the sounds.
Line 4 :
var walk : AudioClip[];
Line 5 :
var run : AudioClip[];
Line 85 :
audio.clip = walk[ Random.Range(0, walk.Length) ];
Line 101 :
audio.clip = run[ Random.Range(0, run.Length) ];
With this it will pick a random sound from the arrays of audio clips and play that each footstep, so at least every footstep doesn't sound exactly the same.
Not for different surfaces, but it makes this script better.
@monsterga$$anonymous$$ghd, 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"
Answer by MonsterGamingHD · Apr 02, 2013 at 05:46 AM
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.
Answer by EliteMossy · Apr 02, 2013 at 09:17 AM
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.