pointlight on/off like in horror games

Hey guy’s. I was looking for a JS were the pointlight goes on/off like in horror movies/games.

By example; 2 seconds light and then it’s off for a half second and then half second on, and then 1 second off and then again 2 seconds on.

2 seconds: on → 0.5 second: off → 0.5 second: on → 1 second: off → Then again and over again.

I was looking but i couldn’t find anything.

I thought something like this;

    function Update() {
    light.range += 5.0 * 0.0;
    light.range +=-5.0 * 2.0;
    light.range += 5.0 * 2.5;
    light.range +=-5.0 * 3.5;
}

But i know it’s wrong :frowning:

Someone help me please.

Thanks in advance.

One of your problems is that you would be making all of these changes in a single frame. That is what the Update() function amounts to each time it is called: a single frame. There are different approaches to spacing out a timed event, but one way might be this:

        float lastTime = 0; // Keeps track of the last time the light was changed
        bool lastState = false; // Keeps track of the last state the light was set to, on (true) or off (false)


void Update () {
    if (lastTime + Random.Range(0.5f, 2f) < Time.realtimeSinceStartup)
    {
        if (lastState == false) // If the light was off...
        {
            light.range = 5; // Turn it on.
            lastState = true;
        }
        else // If the light was on...
        {
            light.range = 0; //Turn it off.
            lastState = false;
        }

        lastTime = Time.realtimeSinceStartup; // And update the last time the light was set
    }
}

Note the Random.Range() function. It will generate a value between 0.5 and 2, which correspond to .5 seconds and 2 seconds. It serves as the interval for the flicker. This ought to be an easier way to get a random stutter to the light.

EDIT:
I just realized that you were looking for javascript. Sorry about that. Still the concept applies, and aside from the way the variables are declared, I don’t think too much of the code is different.

I converted the code for you:

    private var lastTime : float = 0; // Keeps track of the last time the light was changed
    private var lastState : boolean = false; // Keeps track of the last state the light was set to, on (true) or off (false)
    
    
    function Update () {
        if (lastTime + Random.Range(0.5f, 2f) < Time.realtimeSinceStartup)
        {
            if (lastState == false) // If the light was off...
            {
                light.range = 5; // Turn it on.
                lastState = true;
            }
            else // If the light was on...
            {
                light.range = 0; //Turn it off.
                lastState = false;
            }
    
            lastTime = Time.realtimeSinceStartup; // And update the last time the light was set
            }
     }

Why not do it with a coroutine?

var minOnTime : float = 1;
var maxOnTime : float = 2.5;
var minOffTime : float = 0.2;
var maxOffTime : float = 0.6;

// set this in the inspector- drag the actual mesh onto it!
var lampRenderer : MeshRenderer;

private var lightIntensity;
private var glowyTexture : Texture2D;
private var blackTexture : Texture2D;

function Start()
{
    lightIntensity = light.intensity;
    blackTexture = new Texture2D(2, 2);
    var cols : Color[] = new Color[4];
    for( var i = 0; i < cols.Length; ++i ) {
        cols *= Color.clear;*
 *}*
 *blackTexture.SetPixels(cols);*
 *blackTexture.Apply();*
 *glowyTexture = lampRenderer.material.GetTexture("_Illum") as Texture2D;*
 *StartCoroutine(FlickerLoop());*
*}*
*function FlickerLoop()*
*{*
 *while(true)*
 *{*
 *yield WaitForSeconds(Random.Range(minOnTime, maxOnTime));*
 *light.intensity = 0;*
 *lampRenderer.material.SetTexture("_Illum", blackTexture);*
 *yield WaitForSeconds(Random.Range(minOffTime, maxOffTime));*
 *light.intensity = lightIntensity;*
 *lampRenderer.material.SetTexture("_Illum", glowyTexture);*
 *}*
*}*
*```*
*<p>This will make the light randomly flicker by the four values you can change in the inspector. You can also adapt this script to make the light jump around a bit, or make it change colour!</p>*
*<p>(I'm not too hot with JS, since I'm usually a C# man, so tell me if this has any errors since it's not tested!)</p>*
*<p><strong>EDIT:</strong> There was one other issue- it should have been Color.clear, not Color.black where I set the 'black' texture! Sorry again (forgot how illum textures worked)</p>*