- Home /
I want to turn my flashlight on and off, while still maintaining the flicker effects.
Hi guys,
I don't know what I'm doing wrong here but I was hoping that you could help me. Yes, this is unfortunately another "hey im new halp pls ily all xx" messages, but I can honestly say that I gave this my best shot before coming here, and the time you take out to answer my question will be greatly appreciated.
You see, I'm trying to make a Survival Horror game about ghosts. I have a flashlight that I've given the player, but I've realized that it's not really ideal for this kind of situation as it ruins a lot of the ambience.
My conclusion is that I need a dimmer, flickering flashlight, that is accessible but the player essentially finds useless. I am currently able to turn it on and off with the F key using this script:
function Update () {
if (Input.GetKeyDown("f")) {
if (light.enabled == true)
light.enabled = false;
else
light.enabled = true;
}
}
Now my problem is that I'm also using another script for the flicker effect, which to my understanding renders the aforementioned script useless as, when it is applied, checks to see if the light is on. The F key would only work in the few instances that you actually managed to press it during one of the flickers.
var minFlickerSpeed : float = 0.01;
var maxFlickerSpeed : float = 0.1;
var minLightIntensity : float = 0;
var maxLightIntensity : float = 1;
while (true)
{
light.enabled = true;
light.intensity = Random.Range(minLightIntensity, maxLightIntensity);
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
light.enabled = false;
yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
}
function Update () {
if (Input.GetKeyDown("f")) {
if (light.enabled == true)
light.enabled = false;
else
light.enabled = true;
}
}
That is the flicker script that I am using.
Now my immediate reaction would be to create a script to check to see if that script is running, turn it off and turn the light off at the same time upon pressing the F key, and vice versa. It probably wouldn't work, but I don't know where to start in making that script.
What advice do you guys have in which direction I should take?
I'm sorry for the late response.
I actually was not able to follow most of what you had said. I think that I am very behind in the way of Javascript. I'm primarily positioned in computer networking and this is my first attempt at game development.
Both of the responses to this question were too difficult for me to wrap my head around. Perhaps I should start learning some more Java to understand.
I was unsuccessful in applying your knowledge to my game. The flicker effect worked but I still have no idea how to turn it on or off.
Thankyou for taking the time out to try and explain it to me.
Unity uses its own version of JavaScript. What I imagine Unity to be like is having a scene based around objects. Each of these objects are then built with Components. One (or more) of those components can be scripts, with $$anonymous$$onoBehaviour so they can be programmed individually. Then there are other components, from lights to rigidbodys, colliders to materials/renderers, and more =]
Here is a series of video's you may want to watch. From memory they are about 5 $$anonymous$$utes each, and introduce alot of the above ideas, as well as an introduction to using those components and scripting. I shall make a package to demonstrate my answer. (this may take a little while, am busy)
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
Answer by AlucardJay · Sep 11, 2012 at 11:37 AM
EDIT :
I have adapted this script to give a more spooky feel. The torch works fine for 1-2 minutes, then starts to flicker. Pressing F resets the light (light flashes twice as if being tapped/shaken) then light works again for another 1-2 minutes.
This could be adapted so the torch runs out of battery, then pressing F consumes a battery, batterys are placed and then found by the player in the scene. Here is the script :
#pragma strict
var minWorkingTime : float = 60.0;
var maxWorkingTime : float = 120.0;
var minLightIntensity : float = 0.5;
var maxLightIntensity : float = 2.5;
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.F))
{
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;
}
}
As in my first suggestion, use this with batteries stored in your inventory for a real survivor feel =]
Original Answer :
First of all, Fattie has given an excellent answer. I have used Invoke and InvokeRepeating and it is very useful for actions that reoccur regardless to the states of other objects in the scene (as just one example). However I was off writing this answer, and this is what i have created :
Set up your flashlight so it can look after all the behaviours it will have and then set up some booleans or a state engine to manage the flashlight current state, and make those public to be called from other scripts. That way you could enter a room and 'force' the flashlight off with the same command as the player turning the flashlight off.
SO basically, remove all the "light.enabled = " from the player script, and change them to "tell flashlight it's enabled = "
Then instead of having 'while (true)', break the flashlight down so it only has 1 of 2 states.
here is my FlickeringFlashlight.js :
#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 here is the player portion of the script that turns the flashlight on and off. You need to drop your flashlight in the Inspector (this can also be done in-game by assigning the flashlight from the backpack or pickup item) Player.js :
#pragma strict
// ** NOTE :
// ** FlickeringFlashlight is the name of my flashlight script.
// ** Use the name of your script here
public var flashlightScript : FlickeringFlashlight;
function Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
if (!flashlightScript.isFlashlightOn)
{
flashlightScript.isFlashlightOn = true;
}
else
{
flashlightScript.isFlashlightOn = false;
}
}
}
This is an example project to show how I got the player to communicate to the flashlight.
download this package : http://www.alucardj.net16.net/unityanswers/FlickeringFlashlight.unitypackage
In Unity :
Create a New Project, call it FlickeringFlashlight.
Assets > Import Package > Custom Package...
then import the downloaded package.
Open the scene : FlickeringFlashlight
then click Play =]
Have a look at how the player is moved by it's own script, the flashlight script is on the flashlight. All these objects (and camera) are children of the player, so they move with the player. The flashlight object is dragged and dropped into the players Inspector.
To have the Flashlight OFF then Flicker when ON , change the Update to :
function Update()
{
if (isFlashlightOn)
{
FlickerFlashlight();
}
else
{
light.enabled = false;
}
}
Answer by Hexx · Sep 12, 2012 at 02:52 AM
Thanks, I really understand that part of the parent-child object thanks to you. :) However is it possible to make it so there is only off, and flickering? In that sample the flashlight had on, and flickering. My desired effect is to have the flashlight off, and only let it flicker when turned on.
Never $$anonymous$$d, I figured it out. :) I changed light.enabled = true to false in the script. Thanks for the help!
Answer by MrGameplay3 · Nov 11, 2013 at 01:49 AM
hey, can you re-up this like fo me ? http://www.alucardj.net16.net/unityanswers/FlickeringFlashlight.unitypackage