- Home /
Strobe light when a key is pressed
Hi!
I'm making a funfair game, and I need a strobe or flicker light script.
I want a light to strobe/flicker when a key is being pressed. I made a code that flickers the light automatically, but I can't made one yet when a key is being pressed. I hope somebody can help me with this.
using UnityEngine;
using System.Collections;
public class Flitser : MonoBehaviour
{
Light testLight;
public float minWaitTime;
public float maxWaitTime;
void Start()
{
testLight = GetComponent<Light>();
StartCoroutine(Flashing());
}
IEnumerator Flashing()
{
while (true)
{
yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime));
testLight.enabled = !testLight.enabled;
}
}
}
Answer by Hellium · Sep 04, 2019 at 06:43 AM
If you want the light to flicker when a key is held down.
using UnityEngine;
using System.Collections;
public class Flitser : MonoBehaviour
{
Light testLight;
public float minWaitTime;
public float maxWaitTime;
public KeyCode triggerKey = KeyCode.Space;
private float timer = 0;
void Start()
{
testLight = GetComponent<Light>();
timer = Random.Range(minWaitTime, maxWaitTime);
}
private void Update()
{
if( Input.GetKey(triggerKey) )
{
timer -= Time.deltaTime;
if ( timer < Time.deltaTime )
{
timer = Random.Range(minWaitTime, maxWaitTime);
testLight.enabled = !testLight.enabled;
}
}
else
{
timer = 0;
// Following lines optional
// Just in case you always want the light to be enabled
// if the key is not held anymore
if(Input.GetKeyDown(triggerKey))
{
testLight.enabled = true;
}
}
}
}
If you want the light to flicker after a key is pressed down and until another key is pressed down (switch on/off with 2 different buttons)
using UnityEngine;
using System.Collections;
public class Flitser : MonoBehaviour
{
Light testLight;
public float minWaitTime;
public float maxWaitTime;
public KeyCode enableKey = KeyCode.O;
public KeyCode disableKey = KeyCode.L;
private float timer = 0;
private bool flickering;
void Start()
{
testLight = GetComponent<Light>();
timer = Random.Range(minWaitTime, maxWaitTime);
}
private void Update()
{
if( flickering )
{
timer -= Time.deltaTime;
if ( timer < Time.deltaTime )
{
timer = Random.Range(minWaitTime, maxWaitTime);
testLight.enabled = !testLight.enabled;
}
if( Input.GetKeyDown(disableKey) )
{
timer = 0;
flickering = false;
// Following line optional
// Just in case you always want the light to be enabled
testLight.enabled = true;
}
}
else if( Input.GetKeyDown(enableKey) )
{
flickering = true ;
}
}
}
Amazing! This works! Also, I have a few spot lights in different colours, If a user want to enable/disable them with the same or different key.
How can I do that?
So for example, the $$anonymous$$ey "O" enables the lights and the key "L" disables the lights
I didn't understand everything, but I've updated the answer with a new code to enable the flickering with a key, and disable it with another key.
I don't mean a flicker with a different key, I mean just a static light, that goes on/off with the keys "O" and "L" It's hard to explain, English is my second language.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211635im_/https://answers.unity.com/themes/thub/images/avi.jpg)