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 /
avatar image
0
Question by MiguelMNavas · Jun 08, 2019 at 11:27 PM · script.sound effects

ThunderClap

I am trying to follow a course to learn Unity, and I do exactly what appears in the video:

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

public class ThunderClap : MonoBehaviour { bool canFlicker = true; private Light light; public AudioClip clip;

 void Awake()
   {
       light = GetComponent<Light>();
       
   }

   private void Update()
   {
       StartCoroutine(Flicker());

   }

   IEnumerator Flicker()
   {
       if (canFlicker)
       {
           canFlicker = false;
           audio.PlayOneShot(clip);
           light.enabled = true;
           yield return new WaitForSeconds(Random.Range(0.1f,0.4f));
           light.enabled = false;
           yield return new WaitForSeconds(Random.Range(0.1f,5f));
           
           canFlicker = true;
       }
   }

But the sounds is not playing at the same time of the lightning. Anyone to help me?

Comment
Add comment · Show 1
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 BradyIrv · Jun 09, 2019 at 03:08 AM 0
Share

Its not going to change the outcome of the code, but it would also be better to wrap the Startcoroutine in the if(canflicker) statement rather than checking in the coroutine. Because otherwise you are starting a coroutine every frame which then does nothing.

 if(canFlicker)
    Startcoroutine(Flicker());
    canFlicker = false;
 }

 private IEnumerator Flicker() {
    // Do stuff
    canFlicker = true;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BradyIrv · Jun 09, 2019 at 03:02 AM

Does the audio clip (the actual audio file of the thunder sound) play immediately when you listen to it normally? Taking unity out of the equation and only listening to the audio clip itself is there a delay before the Thunder?


I'm thinking that it could be the audio file itself that has a delay because the rest of your code looks fine. Assuming that you can hear it and it's just not the right timing.

Comment
Add comment · Show 5 · 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 MiguelMNavas · Jun 09, 2019 at 07:37 AM 0
Share

Firstly, Thanks!

Unfortunately is not the audio. And no, I am not hearing anything.

About the script, something must be wrong because it is not working. the thing is that I am using this script, but whatever script that allows me to do a lightning at the same time of a thunderclap it will be good. So, maybe someone knows another formula to do it smartly.

avatar image BradyIrv MiguelMNavas · Jun 09, 2019 at 02:05 PM 0
Share

If you're not hearing anything and there are no errors and you've debugged that the Coroutine ran.. then it must be an issue in the audio script. Can you post the code from that? On line 18 you reference the audio script.

avatar image MiguelMNavas BradyIrv · Jun 10, 2019 at 05:09 PM 0
Share

Ok, now I fix the issue of the synchronization between lightning and thunder, but, how can I listen the thunder in every place? Because I am only hearing it when I am at some point in the level.

Thanks!!!

Show more comments

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

119 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

Related Questions

How do I make enemies play a sound when they die? 1 Answer

How to play multiple audio files from one script 1 Answer

How can I make my player stay in the directions i have assigned to it? 0 Answers

Recursive function that allows me to do stuff in the meantime 3 Answers

How do i change the text color on GUI.Label ? 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