- Home /
FlashLight Animation if energy<10??
Hey everyone:), I have a working script for my FlashLight that makes it run out of energy en a while. I'm not good at scripting but i have this idea of adding an animation to the end of the life of the flashlight (It will flicker, low intensity, etc. The animation is done so thats not a problem;)) Is it possible to add to this script an event that tells something like: If energy<10, animation.Play()??? Please Help:)
The Flashlight Script:
//Name this script Flashlight and attach it to your player for instance
var lightSource : Light; //Connect the light source in the Inspector
var shootSound:AudioClip;
static var energy : float = 100; //The energy amount of the flashlight
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var drainSpeed : float = 9.0; //The speed that the energy is drained
lightSource.enabled = false;
function Update () {
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
if(energy>1){
turnedOn=!turnedOn;
audio.PlayOneShot(shootSound);
if (turnedOn && energy>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 && energy>0) {
energy -= 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) {
energy = Mathf.Clamp(energy+amount, 0, 100);
}
Answer by trs9556 · Oct 27, 2013 at 08:34 PM
Change TurnOnAndDrainEnergy() to this logic:
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn && energy>0) {
energy -= drainSpeed*Time.deltaTime;
if(energy < 10){
animation.Play(); //change to what you need it to be.
}
yield;
}
lightSource.enabled = false;
}
You had the logic but could not apply it. Based on that I would assume you did not write this script, nor do you understand it considering how simple this was - hell you even gave me what to put. I suggest learning how to script, it would come in handy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120739im_/https://answers.unity.com/themes/thub/images/avi.jpg)