Search Unity

How can I create a batteryLife degrading script?

Discussion in 'Scripting' started by Control-definition-launch-file, Jun 12, 2019.

  1. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    Okay, so I have this idea for a project of mine. Basically, I would like to have set in-game objects used by the player to use up power. (This wouldn't be a battery for every tool used by the player, the player would have a power source) My original plan was to set a different amount of power usage in each item, add them together, multiply by a set number, and then subtract that final number and the power amount.

    BatteryLife=1000
    BatteryConsumption=0

    (I would have BatteryConsumption have a max limit of 4.)

    So whatever the current batteryconsumption number is, that number would be multiplied by 2.
    Then, the batterylife and final number would subtract constantly.

    (Ex. 4 * 2 = 8 | BatteryLife - 8)

    The problem I'm facing is the fact that I have no clue how I could script this.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Pick a size for your full battery and assign that to a float.

    Every frame, for each item that draws from the battery, if the item is turned on, then subtract the amount of charge per second that item uses times Time.deltaTime from the battery.

    That's the roughest way. Once you have that working and get a feel for it you can wrap it up in more-meaningful objects, such as a perhaps a ChargeManager script, or a BatteryStorage script, depending on how you want to architect it.

    Stick this in a monobehavior and stick it on a game object and press play.

    Code (csharp):
    1. float BatteryCharge;
    2.  
    3. float FlashlightRate = 10.0f;
    4.  
    5. void Start()
    6. {
    7.  BatteryCharge = 1000.0f;    // full charge
    8. }
    9.  
    10. void Update()
    11. {
    12.  float usageThisFrame = FlashlightRate * Time.deltaTime;
    13.  
    14.  BatteryCharge -= usageThisFrame;
    15.  
    16.  Debug.Log( "Charge remaining: " + BatteryCharge);  // spam the charge out
    17.  
    18.  if (BatteryCharge <= 0)
    19.  {
    20.   // do whatever you want when the battery runs out.
    21.  }
    22. }
    Extra credit for displaying the charge using a Unity UI Text element (check for UI tutorials).
     
  3. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    Thanks for the help! The code did what I wanted it to do. All I did was give it the ability to find and turn off the player flashlight. I also limited it from going far into the negatives.
    Code (CSharp):
    1. float BatteryChargeMin = 0.0f;
    2.  
    3.     float BatteryCharge;
    4.  
    5.     float FlashlightRate = 10.0f;
    6.  
    7.     private Light playerLight;
    8.  
    9.     void Start()
    10.     {
    11.       playerLight = GetComponent<Light>();
    12.  
    13.         BatteryCharge = 1000.0f;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         float usageThisFrame = FlashlightRate * Time.deltaTime;
    19.  
    20.         BatteryCharge -= usageThisFrame;
    21.  
    22.         Debug.Log("Charge remaining: " + BatteryCharge);
    23.  
    24.         if (BatteryCharge <= 0)
    25.         {
    26.             playerLight.enabled = false;
    27.         }
    28.  
    29.         else
    30.         {
    31.             playerLight.enabled = true;
    32.         }
    33.  
    34.         if (BatteryCharge <= BatteryChargeMin)
    35.         {
    36.             BatteryCharge = 0f;
    37.         }
    38.     }
    39. }
    Now all I need to figure out is how to give a tool special abilities for the player, a toolbar selection for the tools, a functioning AI system, and improving on the player movement and Mouselook.
     
    Kurt-Dekker likes this.
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657

    Thats all? "walk in da park" mate :D lol. Good luck in your journeys.
     
  5. Control-definition-launch-file

    Control-definition-launch-file

    Joined:
    Jun 12, 2019
    Posts:
    7
    thanks!
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,631
    Just a funny aside: When I read the thread title I thought you were looking for a script to purposely drain mobile user's real-life batteries. I was real curious to see where that would go. :D
     
    Homicide likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Ha! I read that too.

    "Easy, just turn on shadows, set quality to max, jack FPS to 60, and go grab some asbestos gloves to hold your phone."
     
    Homicide likes this.