Created
March 1, 2017 08:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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