x


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.

more ▼

asked Jun 15 '10 at 09:47 AM

Graeme gravatar image

Graeme
53 9 9 15

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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

more ▼

answered Jun 15 '10 at 09:54 AM

Mike 3 gravatar image

Mike 3
30.7k 10 67 255

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 22 '11 at 01:30 PM

Kinglouis33 gravatar image

Kinglouis33
8 4 5 6

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 17 '12 at 07:58 PM

gooncorp gravatar image

gooncorp
11 2 2 3

(comments are locked)
10|3000 characters needed characters left

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...

more ▼

answered Jun 15 '10 at 11:28 AM

Graeme gravatar image

Graeme
53 9 9 15

honestly i would change the collisionSoundEffect script to have the extra audio sources, and use a function like collisionSoundEffect.GetRandomClip() instead of collisionSoundEffect.audioClip to get the random sound from it. You shouldn't have to change the rest of the script to make it play random sounds then. Also - remove the audioSources = new AudioClip[5] thing, it'll wipe whatever clips you add in the inspector

Jun 15 '10 at 11:35 AM Mike 3

Ok thanks, ignore the post below.

Jun 15 '10 at 11:43 AM Graeme

You may consider either posting a new question or editing the existing question to add the new part of the question that came up. Since this is not a forum, posting extra questions as answers (which is what you actually did ;-) ) might be a little confusing for later readers (especially if people answer those "questions in the answers section" and then those answers get voted up and suddenly don't really have the right context anymore ;-) ).

Jun 15 '10 at 01:47 PM jashan

Yeah sorry about that, did it in a hurry without thinking :-S

Jun 15 '10 at 02:30 PM Graeme
(comments are locked)
10|3000 characters needed characters left

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...

more ▼

answered Jun 15 '10 at 11:42 AM

Graeme gravatar image

Graeme
53 9 9 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1060
x599
x542
x60

asked: Jun 15 '10 at 09:47 AM

Seen: 4403 times

Last Updated: Jun 17 '12 at 07:58 PM