- Home /
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.
Answer by Mike 3 · Jun 15, 2010 at 09:54 AM
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
Answer by Graeme · Jun 15, 2010 at 11:28 AM
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...
honestly i would change the collisionSoundEffect script to have the extra audio sources, and use a function like collisionSoundEffect.GetRandomClip() ins$$anonymous$$d 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
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 ;-) ).
Yeah sorry about that, did it in a hurry without thinking :-S
Answer by Graeme · Jun 15, 2010 at 11:42 AM
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...
Answer by Kinglouis33 · Feb 22, 2011 at 01:30 PM
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