- Home /
Flashlight battery script
Hi everyone. I am trying to write a script that will make a spotlight/flashlight run for a certain amount of time, flicker and make a buzzing sound after say 10 minutes and dim slightly, then turn off completely after another 5 minutes. I also tried to make a script so the player could pick up batteries which would replenish the flashlight. So far total failure. I am new to scripting so scripting in JavaScript would be great. Could I have some help with this, please? Thanks for any input.
Answer by jmgek · Jan 17, 2013 at 11:59 PM
 public float FlashlightTime = 1000f; //change the 1000 to add or subtract time. 
 
     private void Start()
         {
             timeStarted = Time.time;      
         }
         private void Update()
         {
         
             if (Time.time > timeStarted+ FlashlightTime)
             {
                //do stuff
         }
     }
This is in c# remeber to mark as answered.
Answer by robertbu · Jan 18, 2013 at 05:07 AM
Here is a starter flashlight battery script. It uses an Animation curve, so you will need to author how you want the flashlight to behave over time. Attach it to a light and click on the public ac property. You can zoom in and change the characteristics of nodes.
 using UnityEngine;
 using System.Collections;
 
 public class Flashlight : MonoBehaviour
 {
     public float fRunTimeFullCharge = 900.0f;
     public AnimationCurve ac = new AnimationCurve();
 
     float fTimer = 0.0f;
     float fMaxIntensity;
     public float chargeLevel; // Can't set level here
     
     public void SetChargeLevel(float fCharge)
     {
         fTimer = (1.0f - Mathf.Clamp01(fCharge)) * fRunTimeFullCharge; 
         Debug.Log (fTimer);
     }
     
     void Start ()
     {
         Light light = GetComponent<Light>();
         fMaxIntensity = light.intensity;
         chargeLevel = 1.0f;
     }
     
     void Update ()
     {
         if (fTimer < 0.0f || fTimer > fRunTimeFullCharge)
             return;
         chargeLevel = fTimer/fRunTimeFullCharge;
         light.intensity = ac.Evaluate (chargeLevel);
         fTimer += Time.deltaTime;
     }
}
And here is the animation curve I used for testing. The flashing is pulled out in the circle.

Great that's fantastic I'll test this out once I'm back on my laptop.
Answer by MasonicGryphon7 · Jan 18, 2013 at 06:25 PM
This is in JavaScript so you know :D
Added a display for the battery also...
 //Name this script Flashlight and attach it to your player for instance
 
 var lightSource : Light; //Connect the light source in the Inspector
 static var maxEnergy : float = 100; //The energy amount of the flashlight
 private var currentPower;
 var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
 var drainSpeed : float = 2.0; //The speed that the energy is drained
 private var alpha : float;               
 private var duration : float = 0.2;     
 private var baseIntensity : float;
 var rectHeight: float;
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
     if(currentPower < maxEnergy/4 && lightSource.enabled){ 
                 var phi : float = Time.time / duration * 2 * Mathf.PI;
                 var amplitude : float = Mathf.Cos( phi ) * .5 + baseIntensity;
                 lightSource.light.intensity = amplitude + Random.Range(0.1, 1.0) ;
         }
         lightSource.light.color = Color(alpha/maxEnergy, alpha/maxEnergy, alpha/maxEnergy, alpha/maxEnergy);
         alpha = currentPower;  
         
         if (turnedOn==true) {
             if(currentPower > 0.0) currentPower -= Time.deltaTime * drainSpeed; 
             if(currentPower <= 0.0) {lightSource.enabled = false;}
         }
         if (turnedOn==false) {
         if(currentPower < maxEnergy) currentPower += Time.deltaTime * drainSpeed/2; 
         }
 }
 
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () {
     turnedOn=!turnedOn;
     if (turnedOn && maxEnergy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
     }
 }
 
 //When the flashlight is turned on we enter a while loop which drains the energy
 function TurnOnAndDrainEnergy () {
     lightSource.enabled = true;
     while (turnedOn && maxEnergy>0) {
        maxEnergy -= drainSpeed*Time.deltaTime;
        yield;
     }
     lightSource.enabled = false;
 }
 
 //This is called from outside the script to alter the amount of energy
 static function AlterEnergy (amount : int) {
     maxEnergy = Mathf.Clamp(maxEnergy+amount, 0, 100);
 }
 
 //Display current battery on your flashlight
 function OnGUI () {
       GUI.Label (Rect(70, Screen.height/rectHeight - 75,150,60), "Battery:   " + maxEnergy.ToString("F0") + "%");
 }
@$$anonymous$$asonicGryphon7 Do I just have to add this to the character/player, because if so, this sint working for me.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                