Can Someone Help Point Out What's Not Working on This Script?

Hi everybody! I’m a little stuck on this script and would like some assistance. This footstep script works, but it seems to play only the footstep audio once when I’m walking, then just never make a sound again until I stop and make that first step.

Can someone please test this script out and tell me what’s wrong with it? I’ve been trying to get around this for quite some time and have failed numerous times…

#pragma strict
 
/**
* Script made by OMA [www.oma.netau.net]
**/
 
var concrete : AudioClip[];
var wood : AudioClip[];
var dirt : AudioClip[];
var metal : AudioClip[];
var glass : AudioClip[];
var sand: AudioClip [];
var snow: AudioClip [];
var floor: AudioClip [];
var grass: AudioClip [];
private var step : boolean = true;
var audioStepLengthWalk : float = 0.7;
var audioStepLengthRun : float = 0.25;
 
 
function OnControllerColliderHit (hit : ControllerColliderHit) {
var controller : CharacterController = GetComponent(CharacterController);
 
if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Concrete"  && step == true || controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
       WalkOnConcrete();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Concrete" && step == true || controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Untagged" && step == true) {
       RunOnConcrete();
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Wood" && step == true) {
       WalkOnWood();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Wood" && step == true) {
       RunOnWood();
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Dirt" && step == true) {
       WalkOnDirt();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Dirt" && step == true) {
       RunOnDirt();
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Metal" && step == true) {
       WalkOnMetal();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Metal" && step == true) {
       RunOnMetal();     
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Glass" && step == true) {
       WalkOnGlass();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Glass" && step == true) {
       RunOnGlass();
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Sand" && step == true) {
       WalkOnSand();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Sand" && step == true) {
       RunOnSand();       
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Snow" && step == true) {
       WalkOnSnow();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Snow" && step == true) {
       RunOnSnow();
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Floor" && step == true) {
       WalkOnFloor();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Floor" && step == true) {
       RunOnFloor(); 
    } else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass" && step == true) {
       WalkOnGrass();
    } else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Grass" && step == true) {
       RunOnGrass();           
 
    }   
}
 
/////////////////////////////////// CONCRETE ////////////////////////////////////////
function WalkOnConcrete() {
    step = false;
    audio.clip = concrete[Random.Range(0, concrete.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnConcrete() {
    step = false;
    audio.clip = concrete[Random.Range(0, concrete.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}   
 
 
////////////////////////////////// WOOD /////////////////////////////////////////////
function WalkOnWood() {
    step = false;
    audio.clip = wood[Random.Range(0, wood.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnWood() {
    step = false;
    audio.clip = wood[Random.Range(0, wood.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
 
/////////////////////////////////// DIRT //////////////////////////////////////////////
function WalkOnDirt() {
    step = false;
    audio.clip = dirt[Random.Range(0, dirt.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnDirt() {
    step = false;
    audio.clip = dirt[Random.Range(0, dirt.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
 
////////////////////////////////// METAL ///////////////////////////////////////////////
function WalkOnMetal() {    
    step = false;
    audio.clip = metal[Random.Range(0, metal.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnMetal() {
    step = false;
    audio.clip = metal[Random.Range(0, metal.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
////////////////////////////////// GLASS ///////////////////////////////////////////////
function WalkOnGlass() {    
    step = false;
    audio.clip = glass[Random.Range(0, glass.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnGlass() {
    step = false;
    audio.clip = glass[Random.Range(0, glass.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
////////////////////////////////// SAND ///////////////////////////////////////////////
function WalkOnSand() { 
    step = false;
    audio.clip = sand[Random.Range(0, sand.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnSand() {
    step = false;
    audio.clip = sand[Random.Range(0, sand.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
////////////////////////////////// SNOW ///////////////////////////////////////////////
function WalkOnSnow() { 
    step = false;
    audio.clip = snow[Random.Range(0, snow.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnSnow() {
    step = false;
    audio.clip = snow[Random.Range(0, snow.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
////////////////////////////////// FLOOR ///////////////////////////////////////////////
function WalkOnFloor() {    
    step = false;
    audio.clip = floor[Random.Range(0, floor.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnFloor() {
    step = false;
    audio.clip = floor[Random.Range(0, floor.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
////////////////////////////////// GRASS ///////////////////////////////////////////////
function WalkOnGrass() {    
    step = false;
    audio.clip = grass[Random.Range(0, grass.length)];
    audio.volume = .1;
    audio.Play();
    yield WaitForSeconds (audioStepLengthWalk);
    step = true;
}
 
function RunOnGrass() {
    step = false;
    audio.clip = grass[Random.Range(0, grass.length)];
    audio.volume = .3;
    audio.Play();
    yield WaitForSeconds (audioStepLengthRun);
    step = true;
}
 
@script RequireComponent(AudioSource)

Hmm, I’ve never used the character controller so most of this will just be me guessing :frowning:

So are you sure your footsteps are being processed accurately for the:

function OnControllerColliderHit (hit : ControllerColliderHit)

Maybe you should set up some kind of counter for each time this occurs so you can make sure it matches up with the individual foot steps?

I almost feel like this kind of thing would be better with animation events so whenever your foot visually looks like it hits the ground for a new step it will trigger an animation event which goes through the logic. If you use animation events you could cast a ray downwards from your foot and determine what kind of surface it is.

You could also set all the audio clips to play only once or even use PlayOneShot();

That way you could avoid the bool logic which I’m guessing is your problem right now assuming the ControllerColliderHit is working correctly.

Also not sure why you would want to start the audio clips at random points, it seems that might sound a little strange or abrupt if for example the clip started at the 99% complete spot, but I could be wrong.

This whole set up looks very strange to me but then again I don’t use Character Controllers so maybe that is a really good set up? But I’m really thinking animation events would be perfect for this kind of thing or even little colliders on your feet maybe with just normal OnTriggerEnter()

Also I use C# so some of that I’m just assuming your syntax is correct.

From documentation:

“OnControllerColliderHit is called when the controller hits a collider while performing a Move.”

Maybe you are just hitting the collider once on your first step (“once per move”) so you have to stop and go for the next hit?

Also not sure why you are getting the character controller component every step shouldn’t you just store that component at start or as a public variable instead of grabbing the component after each collision?

Edited quite a lot lol… k think i finally got it best I can :slight_smile: