- Home /
Question by
casperskyly · Mar 05, 2013 at 07:02 PM ·
mouselightscrollintensityscrollwheel
Light intensity with mouse scrollwheel
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);
}
Comment
Answer by robertbu · Mar 06, 2013 at 08:54 AM
Here is the basis for making a change (untested). This is more than three levels (i.e. the mouse wheel with run the intensity up and down without restriction right now). You'll likely want to put in some bounds checks. Integrate it into Update(). You will need to declare and assign values for 'intensityFactor' and 'drainFactor';
if (turnedOn) {
float f = Input.GetAxis("Mouse ScrollWheel");
lightSource.intensity += f * intensityFactor;
drainSpeed += f * drainFactor;
}