Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by gorevan · Dec 12, 2016 at 11:08 PM · c#audioproceduralfiregenerate

Could anyone help me on how I would make a procedural fire audio script?

Hi guys,

I was just wondering if anyone could help me with this. I have a campfire within my scene and I would like to produce a script that will procedurally generate fire. I would be using an array of audio clips and would want these to be introduced procedurally. I am really struggling with this and I cannot see any tutorials online anywhere for procedural audio in Unity.

If anyone could help I would be so very grateful.

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
1
Best Answer

Answer by TBruce · Dec 13, 2016 at 12:22 AM

I am not sure what you are looking for. From what I understand you want to have the ability to play a list of sounds/clips sequentially and possibly even loop them when all sounds have played. Below is a script that will do just that

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MyAudioManager : MonoBehaviour
 {
     [Tooltip("List of AudioClip components. This list must be set to the sounds to be played and in the order of the list.")]
     public List<AudioClip> audioClipList = new List<AudioClip>();
 
     [Tooltip("The AudioSource component to play the AudioClips.")]
     public AudioSource audioSource;
     
     [Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
     public bool loopClips = true;
 
     [Tooltip("Start playing sound immediately.")]
     public bool playNow = true;
 
     void Start ()
     {
         // make sure we have a reference to the AudioSource
         if (audioSource == null)
         {
             if (audioSource == null)
             {
                 audioSource = gameObject.AddComponent<AudioSource>();
             }
             else
             {
                 audioSource = gameObject.GetComponent<AudioSource>();
             }
         }
         audioSource.playOnAwake = false;
         audioSource.loop = false;
 
         // make sure that alll the AudioClips in the audioClipList are valid by removing any null clips
         audioClipList.RemoveAll(item => item == null);

         if (playNow)
         {
             PlaySequentialSounds();
         }
     }
 
     public void PlaySequentialSounds()
     {
         StartCoroutine(PlaySounds());
     }
 
     public void StopSounds()
     {
         StopCoroutine("PlaySounds");
     }
 
     IEnumerator PlaySounds()
     {
         if (audioClipList.Count > 0)
         {
             for (int i = 0; i < audioClipList.Count; i++)
             {
                 audioSource.PlayOneShot(audioClipList[i]);
 
                 // wait for the lenght of the clip to finish playing
                 yield return new WaitForSeconds(audioClipList[i].length);
             }
         }
         yield return null;
 
         // if loopClips is set to true then call coroutine
         if (loopClips)
         {
             StartCoroutine(PlaySounds());
         }
    }
 }

If you set the variable loopClips to true (the default) the clips will loop when the last clip has been played.

If you set the variable playNow to true (the default) the clips will start to play immediately on startup.

If you do not want the clips to start playing automatically, you can call PlaySequentialSounds() from another script as it is public. You can also stop sounds at anytime by calling StopSounds(). (Note: You will need a reference to script to call either function PlaySequentialSounds() or StopSounds(). Or if you prefer you can make the class static.

I hope this helps.

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 gorevan · Dec 13, 2016 at 12:59 AM 0
Share

This is the sort of idea I was looking for however I want it just to play random sounds at different pitches. For instance. Random burning wood sounds, then some sparking sounds and these would be randomly played within a certain range.

So basically I would have a main audio file which would be a fire sound which would loop. On top of this I would have sparks and small details which would be procedurally generated.

Thank you so much for the help.

avatar image TBruce gorevan · Dec 13, 2016 at 03:20 AM 0
Share

That is not difficult. This class shown here does the following

  1. It allows you to set a main looping sound/clip

  2. It accepts a list of additional sound clips with varied volume levels for.

  3. The additional sound clips are shuffled every time the list starts to play.

Enhancing the script should not be difficult at all if you feel the need to make additions/changes to it.

avatar image gorevan · Dec 13, 2016 at 01:01 PM 0
Share

This is exactly what I was looking for! Thank you so much. There is one thing however whenever I untick Play Now or Play On Awake on the audio source the sounds still play anyways. I was wanting to only play the sounds if they were in a certain range but so just getting the transform of the fire and players position but I can't do that however has the sound just automatically plays

avatar image TBruce gorevan · Dec 14, 2016 at 03:13 AM 0
Share

I am sorry but you were not clear about

I was wanting to only play the sounds if they were in a certain range but so just getting the transform of the fire and players position

and I could only guess. You will need to take the new code listed here and make any necessary modifications/changes/tweaks needed to meet your requirements as I feel that I have answered the original question fully and then some (If you need more you will need to post a new question using the new script).

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

281 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to get the doors to play the opening and closing sounds seperately/individually 1 Answer

Making a mesh inside multiple points 0 Answers

Coroutine and SetFloat not working as expected 0 Answers

Recording with two microphones in Unity C# 0 Answers

Play multiple sounds from one object 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