I have a flashlight script and I want to make the intensity of the light higher and lower with scrolling on 3 levels.
The 3 levels are the lowLight, normalLight and highLight. That is so I can adjust the drainage.
How to do that?
Here’s the script, it might help others too.
var lightSource : Light;
static var energy : float = 100;
private var turnedOn : boolean = false;
var drainSpeedNormalLight : float = 2.0;
var drainSpeedLowLight : float = 2.0;
var drainSpeedHighLight : float = 2.0;
static var hasFlashlight : boolean = false;
var highLight : float = 2.5;
var normalLight : float = 1.2;
var lowLight : float = 0.75;
function Update () {
if (Input.GetKeyDown(KeyCode.F)&& hasFlashlight|| Input.GetMouseButtonDown(1)&& hasFlashlight)
ToggleFlashlight();
if (Input.GetKeyDown(KeyCode.F)&& hasFlashlight|| Input.GetMouseButtonDown(1)&& hasFlashlight)
audio.Play();
}
function ToggleFlashlight () {
turnedOn=!turnedOn;
if (turnedOn && energy>0) {
TurnOnAndDrainEnergy();
} else {
lightSource.enabled = false;
turnedOn = false;
}
}
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
if (Input.GetAxis("Mouse ScrollWheel") && lowLight)
lightSource.intensity += normalLight;
if (Input.GetAxis("Mouse ScrollWheel") && highLight)
lightSource.intensity -= normalLight;
if (Input.GetAxis("Mouse ScrollWheel") && normalLight)
lightSource.intensity += highLight;
lightSource.intensity -= lowLight;
while (turnedOn && normalLight){
energy -= drainSpeedNormalLight*Time.deltaTime;
yield;
}
while (turnedOn && highLight){
energy -= drainSpeedHighLight*Time.deltaTime;
yield;
}
while (turnedOn && lowLight){
energy -= drainSpeedLowLight*Time.deltaTime;
yield;
}
lightSource.enabled = false;
turnedOn = false;
}
static function AlterEnergy (amount : int) {
energy = Mathf.Clamp(energy+amount, 0, 100);
}