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?

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;
		}
	}
}

Thanks, I really understand that part of the parent-child object thanks to you. :slight_smile:
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.

hey, can you re-up this like fo me ?
http://www.alucardj.net16.net/unityanswers/FlickeringFlashlight.unitypackage