Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Graeme · Jun 15, 2010 at 09:47 AM · audiorandomsoundfootsteps

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jun 15, 2010 at 11:35 AM 0
Share

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

avatar image Graeme · Jun 15, 2010 at 11:43 AM 0
Share

Ok thanks, ignore the post below.

avatar image jashan · Jun 15, 2010 at 01:47 PM 0
Share

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

avatar image Graeme · Jun 15, 2010 at 02:30 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by gooncorp · Jun 17, 2012 at 07:58 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Why is my random sound java script not working? 2 Answers

Random Footsteps within CollisionSoundEffects Script 1 Answer

Start ambient audio tracks at random points? 2 Answers

Footstep Sound Error toSample > fromSample 1 Answer

Strange Bug or Code Error with footsteps? -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges