- Home /
Have a 'Torch' script toggle on and off
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;
}
}
Answer by DanFrias · Mar 13, 2013 at 10:48 PM
I would modify the code to allow the CheckForInput function to be performed every Update cycle, allowing it to capture the F input key. When the F input key is detected it will toggle the light.enabled value. This would then require the logic of the switch statement be placed into the CheckForInput function in the instance when the R key is pressed. The required code changers are below.
In the Update function, simply move the CheckForInput function out of the switch, like this:
function Update()
{
switch( currentState )
{
case flashlightState.IsWorking :
IsWorking();
break;
case flashlightState.Flickering :
FlickerFlashlight();
break;
case flashlightState.Resetting :
Resetting();
break;
}
CheckForInput();
}
Then, in the CheckForInput function, add the logic from the Update function for the R key press and add the logic for the F key press.
Update: I had originally omitted a close parenthesis below. Thank to alucardj for pointing this out.
function CheckForInput()
{
if (Input.GetKeyDown(KeyCode.R) && currentState == flashlightState.Flickering)
{
currentState = flashlightState.Resetting;
}
if (Input.GetKeyDown(KeyCode.F) )
{
light.enabled = !light.enabled;
}
}
@LE$$anonymous$$ONVirus99 : it would have been nice if you gave me credit, or even provide the link to where you found my script for future readers.
Answer is excellent, upvoted.
$$anonymous$$y apologies @alucardj I came across this post some time ago and since I've been making the levels i completely forgot to retrace the source of this script. The script should work however, i keep on getting an error message. I dont really know java as of now so could you tell me how to work around this? i seem to get this error a lot Thanks again
yeah, sorry, but it happens all the time. You were just unfortunate enough to get my comment before my first coffee this morning, when I saw it.
With your problem, you have not closed the parentheses in your conditional. Translation : you forgot a )
if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.F ) )
{
light.enabled = !light.enabled;
}
the conditional
if ( )
the argument
Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.F )
DanFrias you forgot a parentheses! But you still get my upvote =]
Now i know :) Works brilliantly. Thank you again for your help!
So I did forget the close parenthesis :). Thanks for correcting that alucardj
Your answer
Follow this Question
Related Questions
Unity,How to make training Target Enemy 1 Answer
[SOLVED] Java Converters In Unity? 2 Answers
Convert this line of javascript to C# (easy) 1 Answer
Creating a file and writing into it? :p 1 Answer
oescape script name? 1 Answer