Skip to content

Instantly share code, notes, and snippets.

@donovankeith
Created March 1, 2017 08:23
Show Gist options
  • Save donovankeith/59d1cd723e4b3fdf23b60a0f75fe3350 to your computer and use it in GitHub Desktop.
Save donovankeith/59d1cd723e4b3fdf23b60a0f75fe3350 to your computer and use it in GitHub Desktop.
Fades light intensity Up/Down when you press the Up/Down arrow keys.
// Fade.cs
// Fades light intensity Up/Down when you press the Up/Down arrow keys.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fade : MonoBehaviour {
// User Inputs
public float fadeDuration = 1.0f;
// Private Variables
Light myLight;
private float startIntensity;
// Use this for initialization
void Start () {
myLight = GetComponent<Light> ();
startIntensity = myLight.intensity;
}
void Update() {
if (Input.GetKeyDown (KeyCode.DownArrow)) {
StartCoroutine(FadeDown ());
} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
StartCoroutine (FadeUp ());
}
}
// Fades Light Intensity Down
IEnumerator FadeDown(){
float fadeStep = myLight.intensity / fadeDuration;
while (myLight.intensity > 0.0) {
myLight.intensity -= fadeStep * Time.deltaTime;
yield return null;
}
}
// Fades Light Intensity Up
IEnumerator FadeUp(){
float fadeStep = startIntensity / fadeDuration;
while (myLight.intensity < startIntensity) {
myLight.intensity += fadeStep * Time.deltaTime;
yield return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment