- Home /
Question by
Benifir · Oct 09, 2012 at 04:07 PM ·
flashlightcombining
Script Combining Help Question
How might I combine these two scripts, or make them work with each other to have the flashlight flicker when the charge is gone?
#pragma strict
public var isFlashlightOn : boolean = false;
private var flickerCounter : float = 0.0;
var minFlickerSpeed : float = 0.01;
var maxFlickerSpeed : float = 0.1;
var minLightIntensity : float = 0.0;
var maxLightIntensity : float = 1.0;
function Start()
{
flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );
}
function Update()
{
if (isFlashlightOn)
{
light.enabled = true;
light.intensity = 1.0;
}
else
{
FlickerFlashlight();
}
}
function FlickerFlashlight()
{
if (flickerCounter < Time.time)
{
if (light.enabled)
{
light.enabled = false;
}
else
{
light.enabled = true;
light.intensity = Random.Range(minLightIntensity, maxLightIntensity);
}
flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );
}
}
and,
//Name this script Flashlight and attach it to your player for instance
var lightSource : Light; //Connect the light source in the Inspector
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 = 10; //The speed that the energy is drained
private var rechargeSpeed : float = 10; //the speed that the energy is recharged
function Update () {
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
turnedOn=!turnedOn;
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;
flicker();
yield;
}
lightSource.enabled = false;
recharge();
}
//When the flashlight is off the flashlight will recharge
function recharge(){
lightSource.enabled = false;
while (turnedOn && energy<100) {
energy += rechargeSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
//When the flashlight is low on charge it will flicker
function flicker(){
while (energy>0 && energy<20) {
this.GetComponent(flashlightflicker).enabled=true;
yield;
}
this.GetComponent(flashlightflicker).enabled=false;
}
I'm just a 3D artist so this isn't the easiest thing :p
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613082108im_/https://answers.unity.com/themes/thub/images/avi.jpg)