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
0
Question by Obliviate · Aug 10, 2013 at 01:03 AM · javascriptarrayrandomsoundlightning

Random Array of sounds for Random Game Object

I want to Randomly create an array of Thunder Sounds to play when the GameObject comes into scene.

In other words, I have a Random Lightning script that works perfectly to summon a Lightning.Prefab into the scene, this prefab also play a single audio with it, but I want it to play a random array of thunder sounds instead.

I am sure we would need to put this on the the Lightning Randomizer Script, so here it is:

 #pragma strict
 //Objects
 var Lightning : GameObject;
 var Box : GameObject;
 
 //Box Attributes
 private var BoxLength : float;
 private var BoxWidth : float;
 private var BoxHeight : float;
 
 //random Values
 private var randX : float;
 private var randZ : float;
 private var randY : float = BoxHeight;
 
 //timer settings
 private var timer : float = 0.0;
 var Mult : int = 0.1; 
 
 function Update () {
 //seting up boundrys
 BoxLength = Box.gameObject.transform.localScale.x;
 BoxWidth = Box.gameObject.transform.localScale.z;
 BoxHeight = Box.gameObject.transform.localScale.y;
 
 //setting up the random values
 randX = Random.Range(0,BoxLength);
 randZ = Random.Range(0,BoxWidth);
 
 //bringing the lightning in and randomizing the x and z
 timer -= Time.deltaTime*Mult;
     if(timer <= 0){
         Instantiate(Lightning);
         Lightning.transform.position.x = randX;
         Lightning.transform.position.z = randZ;
         timer = 1.0;    
     }
 }
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

1 Reply

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

Answer by cdrandin · Aug 10, 2013 at 01:06 AM

Look at this. http://answers.unity3d.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html

Simply make an array of audio sources. Randomize between the array from 0 to length of array - 1.

Then play it, you should consider pausing when to play the audio or else you will get a lot of noises.

Comment
Add comment · Show 1 · 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 Obliviate · Aug 10, 2013 at 01:34 AM 0
Share

Ok, that will work, but now for the finale, how would I make that audi travel to the player for realistic thunder SFX? Would I have to make an empty game object with the sound array, and attach that to the Lightning.PREFAB, if so, what type of script would I use if the player is tagged Player?

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

14 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

Related Questions

Making an Array of Animations Play at Random With No Repeats 3 Answers

Simple array an spawning question 6 Answers

Choosing a random GameObject out of an array? 1 Answer

Using arrays to randomly select an object 0 Answers

Calling random functions 4 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