- Home /
Flashlight GUI and battery pick up.
Hello !
Im working on a game in the dark where you need battery for your flashlight. This is the script for the flashlight.
var Flashlight : Light; //Connect the light source in the Inspector
var LightOn : boolean = true;
var LightDrainSpeed : float = 0.1;
var BatteryLife : float = 0.0;
var MaxBatteryLife : float = 1.5;
function Update () {
if (LightOn && BatteryLife >= 0)
{
BatteryLife -= LightDrainSpeed * Time.deltaTime;
}
Flashlight.light.intensity = BatteryLife;
if(BatteryLife <= 0)
{
Batterylife = 0;
}
if(Input.GetKeyUp(KeyCode.F))
{
if(LightOn)
{
LightOn = false;
}
else if(!LightOn && BatteryLife >= 0)
{
LightOn = true;
}
Lighten();
}
}
function Lighten(){
if(LightOn)
{
Flashlight.light.active = true;
}
else
{
Flashlight.light.active = false;
}
}
I want to make a GUI to show how many percent left to the battery and i want to make a gameObject Battery when you press E the battery life become 100 %. Please and Thank you.
Answer by aldonaletto · Sep 15, 2013 at 02:14 AM
Add this code to your script: it displays the charge in percentage with the GUI system, and also has a function to recharge when the battery is picked up:
function OnGUI(){
var charge = 100 * BatteryLife/MaxBatteryLife;
GUI.Label(Rect(20,20,100,40), charge.ToString("F0")+"%");
}
function ChargeBattery(){ // function called by battery pickup
BatteryLife = MaxBatteryLife; // sets full charge
}
The battery pick up must have a trigger: when the player enters the trigger, its script verifies the key E - if pressed, the pickup charges the flashlight and suicides. The pickup script could be something like this:
EDITED: Oops! My bad: the event should be OnTriggerStay!
function OnTriggerStay(other: Collider){
if (other.tag == "Player" && Input.GetKeyDown("e")){
// call the function ChargeBattery in the player or its children:
other.BroadcastMessage("ChargeBattery");
Destroy(gameObject); // destroy the pickup
}
}
NOTE: The flashlight script must be attached to the player or to one of its children in order to be called by BroadcastMessage.
Thank You ! but for the pick up i put the script on my battery (GameObject) and when i press E in game nothing happen.
This depends on the pickup hierarchy: if the pickup item doesn't have a rigidbody, create an empty gameobject, attach this script to it, add a sphere collider and set its Is Trigger field, then child the battery model to the trigger.
Ok but now i got this error and i try to fix it put without success
UnityException: Input $$anonymous$$ey named: E is unknown
Oops! It should be "e" ins$$anonymous$$d of "E". Answer fixed.
I dont have a error but its dont work. This is wath i have do: Create a empty gamObject, put a sphere collider and ajust the range. Check Trigger and put the script on it. Wath did i do wrong ?
Your answer
Follow this Question
Related Questions
Spotlight is not working 3 Answers
How do I make a camera flash when I click? 0 Answers
Can a lights falloff be removed? 1 Answer
Which setting dictates how far you can be before a light turns off? 0 Answers
Non of the lights are working properly. 2 Answers