So far i have searched and i have found a script which is perfect for what i am doing although there is no toggle on and off. the script is a timer which after a certain amount of time, it flickers the torch then ‘R’ is pressed to reset it. I would like to have ‘F’ as a toggle on and off key, could anyone help me out telling me where that code would go?
Thanks to @alucardj who provided me with this script in his ‘slender like game tutorial’.
private var startIntensity : float = 1.85;
enum flashlightState { IsWorking, Flickering, Resetting }
var currentState : flashlightState;
private var workingTimer : float = 0.0;
private var workingTimeLimit : float = 90.0;
var minFlickerSpeed : float = 0.05;
var maxFlickerSpeed : float = 0.2;
private var flickerCounter : float = 0.0;
private var resetTimer : float = 0.0;
function Start()
{
workingTimeLimit = Random.Range( minWorkingTime, maxWorkingTime );
light.enabled = true;
startIntensity = light.intensity;
currentState = flashlightState.IsWorking;
}
function Update()
{
switch( currentState )
{
case flashlightState.IsWorking :
IsWorking();
break;
case flashlightState.Flickering :
FlickerFlashlight();
CheckForInput();
break;
case flashlightState.Resetting :
Resetting();
break;
}
}
function IsWorking()
{
workingTimer += Time.deltaTime;
if ( workingTimer > workingTimeLimit )
{
flickerCounter = Time.time + Random.Range( minFlickerSpeed, maxFlickerSpeed );
currentState = flashlightState.Flickering;
}
}
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 );
}
}
function CheckForInput()
{
if (Input.GetKeyDown(KeyCode.R))
{
currentState = flashlightState.Resetting;
}
}
function Resetting()
{
resetTimer += Time.deltaTime;
if ( resetTimer > 0.75 )
{
resetTimer = 0.0;
workingTimer = 0.0;
workingTimeLimit = Random.Range( minWorkingTime, maxWorkingTime );
light.enabled = true;
light.intensity = startIntensity;
currentState = flashlightState.IsWorking;
}
else if ( resetTimer > 0.65 )
{
light.enabled = false;
}
else if ( resetTimer > 0.55 )
{
light.enabled = true;
light.intensity = startIntensity;
}
else if ( resetTimer > 0.25 )
{
light.enabled = false;
}
else if ( resetTimer > 0.15 )
{
light.enabled = true;
light.intensity = startIntensity;
}
else if ( resetTimer > 0.05 )
{
light.enabled = false;
}
}