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 Scribe · Dec 07, 2010 at 08:21 PM · yieldspotlightdecreasecharge

Decrease 'Charge' of characters spotlight over time

 var Charge = 100;
 
 var LightOn = 1;
 
 var LightOff = 0;
 
 private var isLightOn = false;
 
 function Update () {
 
 if(Input.GetKeyDown(KeyCode.Q)) {
     isLightOn = !isLightOn;
 }
     if(isLightOn == true) { 
             if(Charge > 0){
             light.intensity = LightOn;
                           }
 }
     else{
         light.intensity = LightOff; 
 
 
         }
 }

Ok so that is my script, at the moment I have a spotlight attached to my character and when I press 'Q' it toggles on and off. What I want is to make it have a power source that will run out after a set amount of time, two minutes for example.

As you can see I have set the charge of my spotlight to 100 and it turns on only if the charge is more than 0 however despite trying InvokeRepeating and yield functions to minus 1 from charge every x seconds I could not find a solution.

Any help you can give me to show me how to slowly decrease the charge would be much appreciated (I will also need to increase it again but i can probably work that out myself with a script for decreasing the charge)

Scribe

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

3 Replies

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

Answer by Statement · Dec 07, 2010 at 08:31 PM

Let there be a max charge value such that Charge is between 0 and MaxCharge. Math.Lerp will give your light a gradient falloff between LightOff and LightOn.

light.intensity = Mathf.Lerp(LightOff, LightOn, Charge / MaxCharge);

Then you can simply reduce the charge such that:

Charge -= Time.deltaTime;

And you might want to make sure that the charge is in the correct interval:

Charge = Mathf.Clamp(Charge, 0, MaxCharge);

This give us functions we can use:

function DrainBattery() { Charge = Mathf.Clamp(Charge - Time.deltaTime, 0, MaxCharge); }

function UpdateLight() { light.intensity = Mathf.Lerp(LightOff, LightOn, Charge / MaxCharge); }

function CheckSwitch() { if (Input.GetKeyDown(KeyCode.Q)) light.enabled = !light.enabled; }

And you can use them in your update like such:

function Update()
{
    CheckSwitch();
    if (light.enabled)
    {
        DrainBattery();
        UpdateLight();
    }
}
Comment
Add comment · Show 2 · 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 Ipsquiggle · Dec 07, 2010 at 09:18 PM 0
Share

This solution reduces the charge by 1 every second. For a different rate of discharge, add a multiplier to the deltaTime, something like Charge - Time.deltaTime * dischargeRate.

avatar image Scribe · Dec 08, 2010 at 05:15 PM 0
Share

thanks for replying, great help. Thanks again

avatar image
1
Best Answer

Answer by Jesus_Freak · Dec 07, 2010 at 08:34 PM

var Charge = 100; var LightOn = 1;

var LightOff = 0;

private var isLightOn = false;

function Update () { Charge -= 1 * Time.deltaTime;// you can change 1 to any number to drain the charge over a shorter amt of time.

if(Input.GetKeyDown(KeyCode.Q)) { isLightOn = !isLightOn; } if(isLightOn == true) { if(Charge > 0) { light.intensity = LightOn; } } else{ light.intensity = LightOff; } }

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 Scribe · Dec 08, 2010 at 05:16 PM 0
Share

thanks a lot for this

avatar image
0

Answer by caderbyshire · Sep 17, 2012 at 09:36 PM

 var Charge : float = 2;
 var dischargeRate : float = 0.00666667;
 var noCharge : float = 0;
 @HideInInspector
 var lightEnabled = false;
 
 function Update()
 {
     if (Input.GetKeyDown(KeyCode.Q))
     {
         lightEnabled = !lightEnabled;
     }
         
     if (lightEnabled == true && Charge > noCharge)
     {
        light.intensity = Charge -= (Time.deltaTime * dischargeRate);
     }
     
     else 
     {
         light.intensity = noCharge;
     }
 }
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

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

1 Person is following this question.

avatar image

Related Questions

Increase/Decrease Value With Time - Problem 4 Answers

How can I use yield in C# 3 Answers

Yield not working in C# 2 Answers

Issue with yield 0 Answers

How to dispaly list of images from server on GUI? 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