I have a script here to run my torch, so it uses battery power when on and none when it is off:
var lightSource : Light;
var lightSourceDim : Light; // This is just another, larger, dimmer light I have in the same position on the flashlight as the other light
static var lightEnergy : float = 200;
static var turnedOn : boolean = false;
private var drainSpeed : float = 1.0;
function Start () {
ToggleFlashLight(); // This is so it immediately starts draining energy from the flashlight
function Update (){
if (Input.GetKeyDown(KeyCode.F))ToggleFlashLight();
}
function ToggleFlashLight () { // To toggle the flashlight on and off
turnedOn != turnedOn;
if(turnedOn && lightEnergy > 0) {
TurnOnAndDrainEnergy();
}else{
lightSource.enabled = false;
lightSourceDim.enabled = false;
}
}
function TurnOnAndDrainEnergy () { // To make the flashlight drain energy when it is on
lightSource.enabled = true;
lightSourceDim.enabled = true;
while (turnedOn && lightEnergy > 0) {
lightEnergy -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
lightSourceDim.enabled = false;
}
function OnGUI () {
GUI.color = Color.green;
GUI.Button(Rect(10, 10, (Flashlight.lightEnergy), 40), "Battery");
}
And this is my battery pickup script:
var batteryPower : int = 20;
function OnTriggerEnter () {
Flashlight.lightEnergy = Flashlight.lightEnergy ++ batteryPower;
}
I attach it to my battery and set the battery as a trigger, yet it tells me there is a compiler error. Halp!
Thanks
EDIT: I also need help to add an audio clip (a torch button click) when ‘F’ is pressed. If anyone can help with that, thanks.