How to implement a pulsating point light with delays in between?

Hi folks,

I’m quite new to Unity, but have a decent amount of knowledge about the basics of programming. I got along in creating a simple top-down 2D game with enemies and pick-ups, but seemd to hit my limits right now.

In theory it sounds quite simple, and maybe it is: create a pulsating point light with delays in between.

With the help of a tutorial from the official Unity site I created a pulsating light, but I simply cannot get my head around how to implement yield and WaitForSeconds. I looked online and understand the basic principle to some degree, but I have absolutely no idea how to implement it. Hope you guys can help me out and give me a basic approach to my problem. Maybe I’m simply lacking the complete overview yet.

This is my code so far for the pulsating light:

using UnityEngine;
using System.Collections;

public class LightToggle : MonoBehaviour
{
	private Light myLight;

	public float fadeSpeed = 2.5f;            
	public float highRange = 10.0f;        
	public float lowRange = 0.5f;       
	public float changeMargin = 0.2f;
	public bool lightOn;    
	private float targetRange;

	public void Awake()
	{
		myLight = GetComponent<Light>();
		myLight.light.range = 0f;
		targetRange = highRange;
		lightOn = true;
	}

	public void Start()
	{

	}

	public void Update()
	{

		if(lightOn)
		{

			myLight.light.range = Mathf.Lerp(myLight.light.range, targetRange, fadeSpeed * Time.deltaTime);

			CheckTargetRange();
		}
		else
			myLight.light.range = Mathf.Lerp(myLight.light.range, 0f, fadeSpeed * Time.deltaTime);
	}

	void CheckTargetRange ()
	{
		if(Mathf.Abs(targetRange - myLight.light.range) < changeMargin)
		{
			if(targetRange == highRange)
				targetRange = lowRange;
			else
				targetRange = highRange;
		}
	}
}

I’m a big fan of keeping it simple.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Light))]
public class PulsingLight : MonoBehaviour
{
	public bool startOn;
	public float maxBrightness;
	public float transitionTime;
	public float peakDelay;

	private bool isLit;

	void Awake()
	{
		isLit = startOn;
		light.intensity = isLit ? maxBrightness : 0;
	}

	void OnEnable()
	{
		StartCoroutine(Transition(!isLit));
	}

	void OnDisable()
	{
		StopAllCoroutines();
		isLit = false;
		light.intensity = 0;
	}

	IEnumerator Transition(bool turnOn)
	{
		float initialBrightness = light.intensity;
		float targetBrightness = turnOn ? maxBrightness : 0;
		float startTime = Time.time;
		float endTime = startTime + transitionTime;

		while (endTime >= Time.time)
		{
			light.intensity = Mathf.Lerp(initialBrightness, targetBrightness, (Time.time - startTime)/transitionTime);
			yield return null;
		}

		isLit = turnOn;
		yield return new WaitForSeconds(peakDelay);
		StartCoroutine(Transition(!isLit));
	}
}

You can declare you brightness value as [SerializeField] and control it from animation.

hey bud, I hope this helps.
Gruffy 2013
using UnityEngine;

    [RequireComponent(typeof(Light))]
    public class Light_Fader : MonoBehaviour
    {
    public float minIntensity = 0.0f;
    public float maxIntensity = 5.0f;
	public float multiplier = 2.0f;
	
	//public float minFlickerSpeed  = 1f;
    //public float maxFlickerSpeed  = 400f;
     
    float random;
     
    void Start()
    {
    random = Random.Range(0.0f, 65535.0f);
    }
     
    void LateUpdate()
    {
    float noise = Mathf.PerlinNoise(random, Time.time * multiplier);
    light.intensity = Mathf.Lerp(minIntensity, maxIntensity, noise);
    }
    }

It doesnt look like you really need explaining to

I’m quite new to Unity, but have a decent amount of knowledge about the basics of programming

but, this simply takes in a min and max value, creates a perlin noise float value from them and passes this to the light intensity.
it updates and changes to produce a random like glow/intensity change.
Hope you like it
gruffy 2013