Playing With Lighting Effects In Unity 2D

Adding the final bit of polish to my Flappy Jetroid game and wasn’t happy with the lighting when you die. Since it was nested inside of the player it just disappears when you die.

unity-light-fade-out-1

Next I played around with the following code, which will put the light at the same coordinates but on the root of the hierarchy:

GameObject light = GameObject.Find("PointLight") as GameObject;
light.transform.parent = null;

Here what that looks like when the light is persistent after the player dies:

unity-light-fade-out-2

While it’s much better it’s still not perfect. So I created this little script to fade down the light:

using UnityEngine;
using System.Collections;
 
public class FadeDownLight : MonoBehaviour {
 
	private float start;
	private float end;
	private float t = 0.0f;
 
	void Start () {
 
		start = light.intensity;
 
		end = light.intensity/5;
	}
 
	void Update () {
			t += Time.deltaTime;
			light.intensity = Mathf.Lerp (start, end, t / 2);
	}
}

And I modified the previous code to apply it dynamically:

GameObject light = GameObject.Find("PointLight") as GameObject;
light.transform.parent = null;
light.gameObject.AddComponent("FadeDownLight");

Here is what the final results look like:

unity-light-fade-out-3

As you can see there is now a nice subtle fade down and it’s less jarring. While you may be thinking this is a lot of effort to put into a Flappy Bird clone I am taking my time to build lots of small, reusable scripts I can put into any of my future games so getting this right in a project that is easy to work in is key to how I can quickly scale up my code library.

Lynda Unity 2D Essential Training

Subscribe To My Mailing List

Want to learn how to make a game? Not sure where to start? Even if you are a seasoned game maker there is still a lot you can learn from my mailing list. I'll be covering tips and tricks for how to build, release and market games each month.

Simply sign up for my mailing list and also get access to a 50% off discount code for my eBooks and other content. I promise to not spam your inbox!

Join Now