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
1
Question by imaethan · May 20, 2012 at 12:42 AM · javascriptcountdownsecondsframestiming

Counting down in seconds opposed to frames

I've been having a browse on here for a while and haven't come up with a way to fix my script. Basically I have an energy bar for a flashlight, it works fine but it counts way too quick, I was wondering how I could slow it down so that I can use seconds instead of frames. If I set the number for the flashenergy to 600 or so the energy bar works correctly but it starts to make the game lag.

 public static var flashon: int = 0;
 public static var flashlevel: float = 0;
 
 function Start () {
     light.enabled = false;
 }
 
 function Update() {
     if (Input.GetKeyDown("f")) {
         audio.Play();
         if (light.enabled == true)
             light.enabled = false;
         else
             light.enabled = true;
         flashon = 1;
     }
     if (flashlevel == 100){
         light.enabled = false;
     }
     if (light.enabled == false){
         flashon = 0;
     }
     if (flashon == 1){
         if (flashlevel <100)
             flashlevel += 1;
         InvokeRepeating ("Countdown", 1.0, 1.0);
     }
     if (flashon == 0){
         if (flashlevel >0)
             flashlevel -= 2;
         InvokeRepeating ("Countdown", 1.0, 1.0);
     }
 }


Any help would be much appreciated.

Comment
Add comment · Show 3
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 syclamoth · May 20, 2012 at 12:44 AM 1
Share

Is 'flashon' meant to be a boolean? This isn't c, you know. Basically, you need to decrement flashLevel by 'Time.deltaTime'- this will make it decrease by 1 every second, regardless of framerate. If you want it to decrease faster, multiply that by some constant factor.

avatar image syclamoth · May 20, 2012 at 12:51 AM 0
Share

Also, thanks Bunny.

avatar image Eric5h5 · May 20, 2012 at 01:16 AM 1
Share
     if (light.enabled == true)
         light.enabled = false;
     else
         light.enabled = true;

can be replaced with

     light.enabled = !light.enabled;

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · May 20, 2012 at 12:57 AM

I guess i know where your lags are comming from. You you even understand what InvokeRepeating does? It runs the specified function automatically in the interval you specified (in your case every second). But you start another one of these calls every frame. So at 60 fps after 1 sec there are 60 InvokeRepeatings running at the same time and the number is growing endless in your case.

Furthermore, where is your "Countdown" function and what is it doing?

Like syclamoth already mentioned you should increment / decrement your float by Time.deltaTime which is the current frame time ( 1 / fps ). So if your game runs at 100 frames per sec. deltaTime will be 0.01. If you add this value every frame to a variable, it adds up to exactly 1.0 after 1 sec.

edit btw: usually you count down when the flashlight is on... It works of course both ways, but usually that value represents the available energy, or not? In your case it's more like a overheating value which is a bit strange for a flashlight ;)

Also avoid == comparisons with float values 100.002312 is not the same as 100.000000

Comment
Add comment · Show 3 · 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 imaethan · May 20, 2012 at 12:18 PM 0
Share

When I have the flashlight off the energy goes back up, I'm rather new to program$$anonymous$$g if you didn't already realise. Thanks for the help, I'll try this now and see if it works :)

avatar image Bunny83 · May 20, 2012 at 12:46 PM 0
Share

Your code is quite messy ;) I guess this is what you want:

 var drainSpeed = 2.0;
 var restoreSpeed = 1.0;
 
 function Update() {
     if (Input.Get$$anonymous$$eyDown("f")) {
         audio.Play();
         light.enabled = !light.enabled; // toggle
     }
     if (light.enabled){
         if (flashlevel > 0.0)
             flashlevel -= drainSpeed * Time.deltaTime;
         else
             light.enabled = false;
     }
     else
     {
         if (flashlevel < 100.0)
             flashlevel += restoreSpeed * Time.deltaTime;
         else
             flashlevel = 100.0; 
     }
 }
avatar image imaethan · May 20, 2012 at 08:38 PM 0
Share

For some reason my comment didn't post earlier. Thanks very much for the help :)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

script countdown 2 Answers

Countdown not counting down 1 Answer

3D number countdown 0 Answers

Countdown/timer in Javascript 3 Answers

Mini Game Timer - Unity 2017 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