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 Benjeta · Jun 28, 2014 at 06:27 PM · audiotriggernpc

Play random sound on NPC when trigger enter

Hi, i'm making a script to play randomly a audio bank i've created.

It's a script attached to a moving NPC wandering in the map.

I want to be able to assign audios clips in the inspector, play a random audio clip on trigger enter, continue sound on trigger exit then stop all the sound if you not in it and when the currently playing audio clip was finished.

If you stay on the trigger, a new sound randomly choosen play in the next 5 seconds.

If you Exit collider and an audio clip is playing and you return in it, the audio clip finish and after 5 seconds play another random sound.

Here my script :

     #pragma strict
 
   var sound01: AudioClip; var sound02: AudioClip; var sound03: AudioClip; var sound04: AudioClip; var sound05: AudioClip; var sound06: AudioClip; var sound07: AudioClip; var sound08: AudioClip; var sound09: AudioClip; var sound10: AudioClip;
 
 
     private var soundArray : AudioClip[];
     private var soundArrayLength : int = 10;
 
     private var executingSound : boolean = false;
     private var inZoneSound : boolean = false;
 
 
     private var soundSource: AudioSource;
 
     function Start () {
  
         soundSource= gameObject.AddComponent(AudioSource);
         soundSource.loop = false;
         soundSource.volume = 1;
 
      
         soundArray = new AudioClip[soundArray.Length];
         soundArray[0] = sound01; soundArray[1] = sound02; soundArray[2] = sound03; soundArray[3] = sound04; soundArray[4] = sound05; soundArray[5] = sound06; soundArray[6] = sound07; soundArray[7] = sound08; soundArray[8] = sound09; soundArray[9] = sound10;
     }
 
     function Update () {
        if (inZoneSound && !executingSound) playRandomSound();
     }
 
  
     function OnTriggerEnter (other : Collider) {
     if (other.tag == "Player"){
        inZoneSound = true;   
     }
     }
 
   
     function OnTriggerExit (other : Collider) {
     if (other.tag == "Player"){
        inZoneSound = false;   
     }
     }
 
     function playRandomSound () {
    
         executingSound = true;
        
      
         var randomSound = Random.Range(0,soundArray.Length);
         soundSource.PlayOneShot(soundArray[randomSound]);
        
      
         yield WaitForSeconds(soundSource.clip.length);
 
         
         yield WaitForSeconds(5);
         executingSound = false;
     }


But he doesn't work Th clip play at start game and never stop ! He make a random but in continue without the trigger collision !

I'm french, sorry for my english ! Any ideas ? ^^

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 Benjeta · Jun 30, 2014 at 01:05 AM

Ok, I figured out with some help.

Here the solution :

 #pragma strict
  
 var sound01: AudioClip; var sound02: AudioClip; var sound03: AudioClip; var sound04: AudioClip; var sound05: AudioClip; var sound06: AudioClip; var sound07: AudioClip; var sound08: AudioClip;var sound09: AudioClip;var sound10: AudioClip;
  
  
 private var soundArray : AudioClip[];
 private var soundArrayLength : int = 10;
  
 private var executingSound : boolean = false;
 private var inZoneSound : boolean = false;
  
 private var soundSource: AudioSource;
  
 function Start () {
  
 soundSource= gameObject.AddComponent(AudioSource);
 soundSource.loop = false;
 soundSource.volume = 1;
  
  
 soundArray = new AudioClip[8];
 soundArray[0] = sound01; soundArray[1] = sound02; soundArray[2] = sound03; soundArray[3] = sound04; soundArray[4] = sound05; soundArray[5] = sound06; soundArray[6] = sound07; soundArray[7] = sound08;soundArray[8] = sound09;soundArray[9] = sound10;
 }
  
 function Update () {
 if (inZoneSound && !executingSound) playRandomSound();
 }
  
  
 function OnTriggerEnter (other : Collider) {
 if (other.tag == "Player"){
 inZoneSound = true;
 }
 }
  
  
 function OnTriggerExit (other : Collider) {
 if (other.tag == "Player"){
 inZoneSound = false;
 }
 }
  
 function playRandomSound () {
  
 executingSound = true;
  
  
 var randomSound = Random.Range(0,soundArray.Length);
 
     var audioClipTemp = soundArray[randomSound];
 soundSource.PlayOneShot(soundArray[randomSound]);
 //Debug.Log("audio source length " + audioClipTemp.length);
 
 
 yield WaitForSeconds(audioClipTemp.length);
 yield WaitForSeconds(5);
  
  
 executingSound = false;
 }

Thanks anyway ;)

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

2 People are following this question.

avatar image avatar image

Related Questions

Trying to play other objects' sound and animations from Raycast hit 1 Answer

Get an object to play a sound if player rolls over a trigger. 2 Answers

Play audio only once in trigger collision entry? 1 Answer

Adding Sound To Collectable Object 1 Answer

Why isnt OnTriggerEnter Tags working. 1 Answer


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