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 /
This question was closed Jun 07, 2019 at 11:35 AM by shpakrecords for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by shpakrecords · Jun 06, 2019 at 05:26 PM · particlessoundparticle systemsound effectsorder-of-execution

Cant get sound playing BEFORE event happened.

It is the first time I am encountering this issue. I am making a script that changes the effect of wind on a Player after X amount of time and which lasts for Y amount of time. I need my sound to play together with this effect, however, it always plays only AFTER the event finishes. Here is my script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SnowScript : MonoBehaviour { public static SnowScript instance;

 AudioManager audioManager;
 IndentScript indentScript;

 Vector2 originalPos = new Vector2(-6.51f,7.53f);
 Quaternion originalRot = Quaternion.Euler(110,-90,-90);
 ParticleSystem ps;

 public float delay;
 [Range(0f, 2f)]
 public float randomDelay;
 public bool swapped;
 public float snowAffect;
 public float timer;
 public float burstDelay;
 [Range(0f, 2f)]
 public float randomBurstDelay;
 public float snowBurstAffect;
 public bool burst;
 public float burstTimer;
 public float burstActiveTime;

 void Awake()
 {
     instance = this;
     timer = delay;
     burstTimer = burstDelay;
     transform.position = originalPos;
     transform.rotation = originalRot;
     ps = GetComponent<ParticleSystem>();
 }
 void Start()
 {
     audioManager = AudioManager.instance;
     indentScript = IndentScript.instance;
 }
 void Update()
 {
     CheckTimers();
     CheckSwap();
 }
 void CheckTimers()
 {
     if (timer >= 0)
     {
         timer -= Time.deltaTime;
     }
     if (timer <= 0)
     {
         swapped = !swapped;
         Swap();
     }
     if (burstTimer > 0)
     {
         burstTimer -= Time.deltaTime;
     }
     if (burstTimer <= 0)
     {
         burst = true;
     }
 }
 void CheckSwap()
 {
     if (swapped)
     {
         if (!burst)
         {
             var main = ps.main;
             main.simulationSpeed = 1f;
             indentScript.transform.Translate(Vector3.left * snowAffect * Time.deltaTime);
         }
         if (burst)
         {
             var main = ps.main;
             main.simulationSpeed = 3f;
             StartCoroutine("Wait");
             indentScript.transform.Translate(Vector3.left * snowBurstAffect * Time.deltaTime);
         }
     }
     if (!swapped)
     {
         if (!burst)
         {
             var main = ps.main;
             main.simulationSpeed = 1f;
             indentScript.transform.Translate(Vector3.right * snowAffect * Time.deltaTime);
         }
         if (burst)
         {
             var main = ps.main;
             main.simulationSpeed = 3f;
             StartCoroutine("Wait");
             indentScript.transform.Translate(Vector3.right * snowBurstAffect * Time.deltaTime);
         }
     }
 }
 void Swap()
 {
     if (!swapped)
     {
         transform.position = originalPos;
         transform.rotation = originalRot;
     }
     if (swapped)
     {
         transform.position = new Vector2(10.91f, 7.53f);
         transform.rotation = Quaternion.Euler(20, -90, -90);
     }
     timer = delay * (1 + Random.Range(-randomDelay / 2f, randomDelay / 2f));
 }
 IEnumerator Wait()
 {
     //audioManager.PlaySound("WindBurst");  //    - Sound should play BEFORE the WaitForSeconds, however it only plays after.
     yield return new WaitForSeconds(burstActiveTime);
     burst = false;
     burstTimer = burstDelay * (1 + Random.Range(-randomBurstDelay / 2f, randomBurstDelay / 2f));
 }

}

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

  • Sort: 
avatar image
0
Best Answer

Answer by shpakrecords · Jun 07, 2019 at 11:34 AM

Figured out by myself. Apparently, sound does not want to play if it is simply in update. Added an extra condition to check:

void PlayAudio() { if (burst && windBurst.isPlaying == false) { windBurst.Play(); } if (!burst && windBurst.isPlaying) { windBurst.Stop(); } }

Maybe this will help somebody!

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

Follow this Question

Answers Answers and Comments

187 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

Related Questions

how to stop particles shrink texture 1 Answer

Particle system error in world space 0 Answers

How can you create non circular rocket flame particle effect? 0 Answers

Few AudioSources as components on one gameobject 1 Answer

Is there a way I can control parameters within a shader via the particle system 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