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 robbiek · Jan 13, 2012 at 03:01 PM · variabletimeintegervar

Decrease int variable over time

Basically without going into too much detail about my project I'm writing a horror survival/3rd person shooter. (its kind of top secret) lol. I have a flashlight that I got working so far when f is pressed it turns on/off plays a little clicking sound, but now I want to add battery power. Kind of like if flashlight is turned off drain power by 5 every 5 seconds, and what not. when its turned off increase power by 5 every 5 seconds if it goes below 1 disable until full again. etc. I can do the code I'm just a little lost at how I would start with decreasing the power variable if flashlight is enabled over time. the code I have so far is this (its in JS btw)

var flashlight : Light;

var flashlightsound : AudioClip;

var flashlightMaxPower = 100;

var flashlightCurPower = 100;

function Update () {

if (Input.GetKeyDown(KeyCode.F)){

flashlight.enabled = !flashlight.enabled;

audio.clip = flashlightsound;

audio.Play();

}

// Write the code for the battery power

}

any help would be much apreciated.

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

4 Replies

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

Answer by save · Jan 13, 2012 at 03:12 PM

Or if you want a more optimized solution than Time.deltaTime, use invokes.

var flashlight : Light; var flashlightMaxPower : int = 100; var flashlightCurPower : int = 100;

function Start () { InvokeRepeating("FlashlightPower", 5, 5); }

function FlashlightPower () { if (flashlight.enabled) { if (flashlightCurPower>0) flashlightCurPower-=5; } else { flashlight.enabled = false; } } else { if (flashlightCurPower<flashlightMaxPower)+=5; } }

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

Answer by FLASHDENMARK · Jan 13, 2012 at 03:13 PM

It would be easiest if you converted your flashlightCurPower to a float value. By doing so can easily reduce the float by Time.deltaTime, which will result in a decrease of 1 ever seconds. You cannot decrease a integer value with Time.deltaTime since it is of a float value.

 var flashlightCurPower : float = 100;
 
     function Update (){
         if(flashlight is on){
             flashlightCurPower -= Time.deltaTime;
         }
         flashlightCurPower = Mathf.Clamp(flashlightCurPower, 0 flashlightMaxPower); // Make sure the battery level does not become less than 0 or max than 'flashlightMaxPower'
     }

Some thing like that would my way to go.

Comment
Add comment · Show 1 · 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 Skyline Studios · Sep 27, 2012 at 08:31 PM 0
Share

how would I Add This To $$anonymous$$y Flash Light ($$anonymous$$y One Is Called Flashlight)

avatar image
0

Answer by Larry-Dietz · Jan 13, 2012 at 03:09 PM

To give you a point to get started, look at time.deltaTime. It will give you the time that has passed since the last update. If you are wanting to decrement power by 5 every 5 seconds, create a variable to store elapsed time and increment it by time.deltaTime each update, then check to see if it is > 5, if so, decrement your power, and reset your variable back to 0.

Hope this helps, -Larry

Comment
Add comment · Show 1 · 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 robbiek · Jan 13, 2012 at 03:10 PM 0
Share

Thank you, I will give that a shot.

avatar image
0

Answer by astracat111 · Mar 11, 2018 at 02:55 AM

Use floats and use this:

    if (flashlightCurPower > 0) 
         flashlightCurPower = flashlightCurPower - 5f * (60f * Time.deltaTime);
     } else if (flashlightCurPower <= 0) {
         flashlight.enabled = false;
     }
Comment
Add comment · Show 1 · 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 Bunny83 · Mar 11, 2018 at 10:07 AM 0
Share

Why "5 * 60"? You basically just say "300" which would be more natural. Which means you subtract 300 every second.

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

10 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

Related Questions

Delaying a dynamic Variable(transform.position for Ex.) 2 Answers

Creating a variable jump using deltaTime in C#. 0 Answers

timed value increase? 1 Answer

Time variables not interacting 1 Answer

Yielding for a changing amount of time? 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