Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JohnWatson · Sep 26, 2013 at 09:31 AM · javascriptaudiorandomsound

Sound playing at random. (JS)

Hey, I have a script running certain sounds when I do certain things.Besides that I want to be able to have different Soundclips playing in a random order at random times. (The game we're making is trying to be creepy so those Soundclips could be things like Screams etc.)

I would preferably want to have that in the same script to make things less complicated. Here's what I have now:

 #pragma strict
 
 var moveSound : AudioClip;
 var landSound : AudioClip;
  
 private var hasJustLanded = true;
 
 var controller : CharacterController = GetComponent(CharacterController);     
  
 function Start () {
     audio.clip = moveSound;
 }
  
 function Update()
 {
     var isMoving = Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d");
     if (controller.isGrounded) {
        if (isMoving) {
          if (!audio.isPlaying) {
           audio.Play();
          }
        }
        else if (audio.isPlaying) audio.Stop();
        if (!hasJustLanded) {
          hasJustLanded = true;
          
          AudioSource.PlayClipAtPoint(landSound, transform.position);
        }
     }
     else {
         if (audio.isPlaying) audio.Stop();
         hasJustLanded = false;
     }
 }

I appreciate any Help. Thanks in advance.

Comment
Add comment · Show 10
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 vexe · Sep 26, 2013 at 09:52 AM 0
Share

Welcome back! :D - So you just have different sound clips, that you want to play at random?

avatar image JohnWatson · Sep 26, 2013 at 09:59 AM 0
Share

I'm glad to be back in your arms,haha :D. Yeah we're not realy sure what we want to do with it yet but I'm thinking I'll hae a Soundclip per level to play at random. I'm going to be gone for about 40 $$anonymous$$utes I'll be back.

avatar image vexe · Sep 26, 2013 at 10:02 AM 0
Share

You mean you have multiple clips that you want to choose one of them randomly and play? Or one clip that you want to play at random times?

avatar image JohnWatson · Sep 26, 2013 at 10:08 AM 0
Share

Well it's not really my idea I'm just the one responsible for sound. but I think they mean they want different sounds to play at random times in a random order. But I think tha t one sound at random times is also possible.

avatar image vexe · Sep 26, 2013 at 10:12 AM 2
Share

you're not sure you know what you want? - I can help you if you be a bit more specific - Go ask them and make sure what exactly is required. - And make sure you add the details to your question, as it's very ambiguous as it is.

Show more comments

2 Replies

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

Answer by vexe · Sep 26, 2013 at 11:35 AM

1- Create an array of clips, assign its clips from the inspector.

 var clips : AudioClip[];

2- Add those to what you currently have, and stick PlayRandomClip where you want to play the sounds.

 var randPercentage : float = 5; // by default, 50% chance of playing, change this to suit your needs, 0 means 0%, 1 means 10%, 8 means 80%, etc.

 function PlayRandomClip()
 {
     var randClip = clips[Random.Range(0, clips.Length)]; // choose a clip at random
     if (GetRandChanceOfPlaying())
         AudioSource.PlayClipAtPoint(randClip, transform.position); // assuming that that's where you like to play the sound from
 }

 // This will return true 'randPercentage' % of the time
 function GetRandChanceOfPlaying()
 {
     // just making sure randPercentage stays in [0, 10]
     randPercentage = Mathf.Clamp(randPercentage, 0, 10);
     return (Random.Range(0, 10) < randPercentage);
 }
Comment
Add comment · Show 11 · 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 vexe · Sep 26, 2013 at 11:53 AM 0
Share

Let me know how it goes...

avatar image JohnWatson · Sep 26, 2013 at 12:23 PM 0
Share

I'm getting comipling errors.

 function //expects ( is$$anonymous$$d of PlayRandomClip & expects a semicolon
 {
     var randClip = clips[Random.Range(0, clips.Length-1)];
     if (GetRandChanceOfPlaying())
         AudioSource.PlayClipAtPoint(randClip, transform.position);
 }
 
 function GetRandChanceOfPlaying()//expects semicolon

Semicolons shouldn't be needed at the end of a variable right?

avatar image vexe · Sep 26, 2013 at 12:45 PM 0
Share

You don't need a ';' after a function declaration no. I'm not seeing where there could be an error. Although I just had a debate on Random.Range, I thought it was inclusive on both ends, it turns out it's inclusive in the beginning end, and exclusive at the right end (ONLY FOR THE INT VERSION) while in the float version both ends are inclusive, see the docs. (Very confusing and stupid in my opinion) - I updated accordingly.

avatar image JohnWatson · Sep 26, 2013 at 12:47 PM 0
Share

am I supposed to do this in a different script? or will this work in the same one?. I guess I should've lead with that.

avatar image vexe · Sep 26, 2013 at 12:50 PM 0
Share

Same one will work just fine.

Show more comments
avatar image
0

Answer by Sisso · Sep 26, 2013 at 11:26 AM

How to store more that one variable

http://docs.unity3d.com/Documentation/ScriptReference/Array.html

How to get random values

http://docs.unity3d.com/Documentation/ScriptReference/Random.html

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Random Audioclip. No Repeat ?? 1 Answer

Audio not playing [Fixed] 1 Answer

Audio Needs to stop 1 Answer

Can't get a sound to loop 2 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