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 Barachiel · Dec 30, 2013 at 05:15 AM · audiotimercountdown

Timer inexplicably counting down slower after it reaches a certain number.

Hi there, I recently encountered a problem that I can't seem to even begin figuring out, so any help would be greatly appreciated.

I have a script specifically for the breathing sound effects of a running character, and I have a timer that uses Time.deltaTime to determine the volume of the effects based on how long the character has been running. When the character can no longer run, another script stops that action and in this script the volume timer begins counting back down.

All of this works perfectly and just how I expect, except when the timer reaches 9 (which is where I've capped it). When this happens, the timer starts counting down agonizingly slow instead of it's normal rate.

EDIT: It just occurred to me that it might be easier for everyone if I pointed out what variable is playing up here. volumeTimer is the one.

Here's the script:


 #pragma strict
 @script RequireComponent(AudioSource)
 
 var breath1 : AudioClip;
 var breath2 : AudioClip;
 var breath3 : AudioClip;
 var breath4 : AudioClip;
 var breath5 : AudioClip;
 var breath6 : AudioClip;
 var breath7 : AudioClip;
 var breath8 : AudioClip;
 var breathEnd : AudioClip;
 
 
 
 var timer = 0f;
 var whichBreath : int;
 var volume = float;
 var volumeTimer = 0f;
 var outOfBreathTriggered = false;
 
 private var devMvmnt : DevMovement;
 
 
 function Start () {
 whichBreath = Random.Range(1,8);
 devMvmnt = transform.root.GetComponent(DevMovement);    //Fetches the sprinting mechanics to stop heavy breathing when sprinting is no longer available.
 }
 
 function Update () {
 
 if (Input.GetKey("w") && Input.GetKey("left shift") && devMvmnt.sprintTimer < 9){
     timer += Time.deltaTime;
     volumeTimer += Time.deltaTime;
     audio.volume = volumeTimer / 60;    //Sets the volume for the breathing sound effect. Longer you run, the louder it gets.
     
     if (whichBreath == 1 && timer >= .7){
         audio.clip = breath1;
         audio.Play();
     }
     else if (whichBreath == 2 && timer >= .7){
         audio.clip = breath2;
         audio.Play();
     }
     else if (whichBreath == 3 && timer >= .7){
         audio.clip = breath3;
         audio.Play();
     }
     else if (whichBreath == 4 && timer >= .7){
         audio.clip = breath4;
         audio.Play();
     }
     else if (whichBreath == 5 && timer >= .7){
         audio.clip = breath5;
         audio.Play();
     }
     else if (whichBreath == 6 && timer >= .7){
         audio.clip = breath6;
         audio.Play();
     }
     else if (whichBreath == 7 && timer >= .7){
         audio.clip = breath7;
         audio.Play();
     }
     else if (whichBreath == 8 && timer >= .7){
         audio.clip = breath8;
         audio.Play();
     }
     if (timer >= 0.7){
         timer = 0.0;
         whichBreath = Random.Range(1,8);
     }
 }
 
 if (volumeTimer >= 9 || audio.volume >= .2){
     volumeTimer = 9;
     audio.volume = .2;                //Sets the maximum volume.
 }
 
 if (devMvmnt.sprintTimer >= 9 && outOfBreathTriggered == false){        //Plays the ending sound for catching the breath when you can't run anymore.
     audio.clip = breathEnd;
     audio.Play();
     outOfBreathTriggered = true;
 }
 if (devMvmnt.sprintTimer <= 6){
     outOfBreathTriggered = false;
 }
 
 if (!Input.GetKey("left shift")){            //Reduces the breathing volume over the time you're not sprinting.
     timer = 0.0;
     volumeTimer -= Time.deltaTime * 1.5;
     if (volumeTimer <= 0){
         volumeTimer = 0;
     }
 }
 }
Comment
Add comment · Show 2
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 Eric5h5 · Dec 30, 2013 at 08:00 AM 0
Share

You could simplify that massively by using arrays.

avatar image Barachiel · Dec 30, 2013 at 09:06 AM 0
Share

I'm pretty new to all of this, so thanks for the info. I'll look into it.

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Play Audio at the end of a Countdown 0 Answers

How can I play a sound in sync with a countdown? 1 Answer

C# countdown timer 9 Answers

Display countdown timer in Unity: distance traveled C# 1 Answer

Countdown 2 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