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 SYRSA · Aug 28, 2013 at 09:42 PM · audiosourcesounds

How can I start at a random point in an audio clip?

I have a bunch of creatures that comes in a swarm, and each of them have the same audio clip which is them making noises.

So when I let them go, they all play the clip at the very start which makes it very loud and unrealistic. Is there a way to pick a random point in the clip so it gets all distributed?

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
2

Answer by TrickyHandz · Aug 28, 2013 at 11:24 PM

Rather than picking a random point in the clip you can delay the start of the clip. The easiest way to do this is to make sure the each objects AudioSource does not have "Play On Awake" enabled and delay the start of the clip through your script. Below is an example of how to assign a random delay to each creature starting to play the clip:

JavaScript Example:

 #pragma strict
 
 // Make sure there is an AudioSource
 // attached to this object
 @script RequireComponent(AudioSource)
 
 // The minimum and maximum delay
 // value in seconds
 var minDelay : float = 0.0;
 var maxDelay : float = 5.0;
 
 function Start () 
 {
     // Just to make sure we are
     // going to disable the play
     // on awake for the AudioSource
     audio.playOnAwake = false;
     
     // Make sure there is an
     // AudioClip to play before
     // continuing 
     if(audio.clip)
     {
         var delayTime : float = Random.Range(minDelay, maxDelay);
         audio.PlayDelayed(delayTime);
     }
 }

C# Example:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 public class DelayAudio : MonoBehaviour 
 {
     
     public float minDelay = 0.0f;
     public float maxDelay = 5.0f;
     
     void Start () 
     {
         
         // Just to make sure we are
         // going to disable the play
         // on awake for the AudioSource
         audio.playOnAwake = false;
     
         // Make sure there is an
         // AudioClip to play before
         // continuing 
         if(audio.clip != null)
         {
             // Get a random number between
             // our min and max delays
             float delayTime = Random.Range(minDelay, maxDelay);
             // Play the audio with a delay
             audio.PlayDelayed(delayTime);
         }
     }
     
 }

The scripts above have a maximum delay of 5 seconds by default, but you can easily change that to suit your needs.

EDIT: Corrected conditional check for audio clip in C# code to make it check for null reference.

Comment
Add comment · Show 2 · 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 SYRSA · Aug 28, 2013 at 11:31 PM 0
Share

I say! Thank you very much, my good man.

avatar image TrickyHandz · Aug 28, 2013 at 11:31 PM 0
Share

No problem. Please mark the answer as accepted if it works for you.

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

16 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

Related Questions

Scroll rect sound problem 0 Answers

Multiple audio sources on a game object, how to script to play sequentially? 1 Answer

Several audiosources play at the same time 1 Answer

Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers

Can play multiple sounds from one audiosource on an object. But cant play sound from audiosource on another? 0 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