Random footsteps

Hey, I'm looking to randomly select audio clips from a selection of footstep sounds in order to avoid annoying repetition. I'm not really a programmer but I can edit a template or example to apply it to what I want it to do. There is a tutorial on how to play one audio clips on footstep collision but how would I get it to select one from a few randomly? I can only work out how to attach one audio clip to an object.

If someone could give me an example of some code and just be as patronising as possible I would be most grateful! Thanks.

You can do something like this:

var audioSources : AudioSource[];

function OnCollisionEnter(collision : Collision) //or whatever you have now
{
    //when you get to the part where you want to play the sound...
    var nextClip = audioSources[Random.Range(0, audioSources.Length)];
    audio.clip = nextClip;
    audio.Play();
}

You just fill in audioSources in the inspector with the clips you need

Thanks a lot that seems to work perfectly!

One more thing...

This is the current script I have for playing a single audio source and varying the pitch and volume.

var baseFootAudioVolume = 1.0;

var soundEffectPitchRandomness = 0.05;

function OnTriggerEnter (other : Collider) { var collisionParticleEffect : CollisionParticleEffect = other.GetComponent(CollisionParticleEffect);

if (collisionParticleEffect) {
    Instantiate(collisionParticleEffect.effect, transform.position, transform.rotation);
}

var collisionSoundEffect : CollisionSoundEffect = other.GetComponent(CollisionSoundEffect);

if (collisionSoundEffect) {
    audio.clip = collisionSoundEffect.audioClip;
    audio.volume = collisionSoundEffect.volumeModifier * baseFootAudioVolume;
    audio.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    audio.Play();       
}

}

function Reset() { rigidbody.isKinematic = true; collider.isTrigger = true; }

@script RequireComponent(AudioSource, SphereCollider, Rigidbody)

and the script I've made for randomly selecting audio clips (with your help) is this

var audioSources : AudioClip[];

function Start () { audioSources = new AudioClip[5]; }

function OnCollisionEnter(collision : Collision) //or whatever you have now { //when you get to the part where you want to play the sound... var nextClip = audioSources[Random.Range(0, audioSources.Length)]; audio.clip = nextClip; audio.Play(); }

How would I combine the two for one script that randomly selects audio clips and randomises the pitch and volume...

Ok that was hard to read I'll repost:

Thanks a lot that seems to work perfectly!

One more thing...

This is the current script I have for playing a single audio source and varying the pitch and volume.

var baseFootAudioVolume = 1.0;
var soundEffectPitchRandomness = 0.05;

function OnTriggerEnter (other : Collider) {
    var collisionParticleEffect : CollisionParticleEffect = other.GetComponent(CollisionParticleEffect);

    if (collisionParticleEffect) {
        Instantiate(collisionParticleEffect.effect, transform.position, transform.rotation);
    }

    var collisionSoundEffect : CollisionSoundEffect = other.GetComponent(CollisionSoundEffect);

    if (collisionSoundEffect) {
        audio.clip = collisionSoundEffect.audioClip;
        audio.volume = collisionSoundEffect.volumeModifier * baseFootAudioVolume;
        audio.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
        audio.Play();       
    }
}

function Reset() {
    rigidbody.isKinematic = true;
    collider.isTrigger = true;
}

@script RequireComponent(AudioSource, SphereCollider, Rigidbody)

and the script I've made for randomly selecting audio clips (with your help) is this:

var audioSources : AudioClip[];

function Start ()
{
    audioSources = new AudioClip[5];
}

function OnCollisionEnter(collision : Collision) //or whatever you have now
{
    //when you get to the part where you want to play the sound...
    var nextClip = audioSources[Random.Range(0, audioSources.Length)];
    audio.clip = nextClip;
    audio.Play();
}

How would I combine the two for one script that randomly selects audio clips and randomises the pitch and volume...

Hey mike I've been trying to use the advice that you have given to change the collisionsoundeffect script but no extra audio clips become available would you mind writing out the script for me as i am definitely a scripting thicko

Cheers Matt

how about if (Random.value < .5) {} else {}