Created
December 30, 2015 06:43
-
-
Save DuFF14/4f4bc20ad38c87e062ef to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System.Collections; | |
public class TransparencyFlash : MonoBehaviour { | |
public KeyCode increaseOpacityKey = KeyCode.E; | |
public KeyCode decreaseOpacityKey = KeyCode.Q; | |
public KeyCode increaseFrequencyKey = KeyCode.C; | |
public KeyCode decreaseFrequencyKey = KeyCode.Z; | |
public float opacity = 0.1f; | |
public int flash_freq = 25; | |
private float minOpacity = 0f; | |
private float maxOpacity = 1f; | |
private float fadeSpeed = .1f; | |
private int minFrequency = 0; | |
private int maxFrequency = 75; | |
private float frequencyUpdateDelay = .1f; | |
private Material _material; | |
private MeshRenderer m; | |
int timer = 0; | |
// Use this for initialization | |
void Start () { | |
m = GetComponent<MeshRenderer>(); | |
_material = GetComponent<MeshRenderer>().material; | |
Color c = _material.color; | |
c.a = opacity; | |
_material.color = c; | |
} | |
void Update () { | |
OpacityControls(); | |
FrequencyControls(); | |
Timer(); | |
} | |
void OpacityControls() | |
{ | |
if (Input.GetKey(increaseOpacityKey)) | |
{ | |
Color c = _material.color; | |
opacity += Time.deltaTime * fadeSpeed; | |
opacity = Mathf.Clamp(opacity, minOpacity, maxOpacity); | |
c.a = opacity; | |
_material.color = c; | |
} | |
if (Input.GetKey(decreaseOpacityKey)) | |
{ | |
Color c = _material.color; | |
opacity -= Time.deltaTime * fadeSpeed; | |
opacity = Mathf.Clamp(opacity, minOpacity, maxOpacity); | |
c.a = opacity; | |
_material.color = c; | |
} | |
} | |
void FrequencyControls() | |
{ | |
if (Input.GetKeyDown(increaseFrequencyKey)) | |
{ | |
flash_freq++; | |
} | |
if (Input.GetKeyDown(decreaseFrequencyKey)) | |
{ | |
flash_freq--; | |
} | |
flash_freq = Mathf.Clamp(flash_freq, minFrequency, maxFrequency); | |
} | |
void Timer() | |
{ | |
timer++; | |
if (timer >= flash_freq) | |
{ | |
Color c = _material.color; | |
c.a = opacity; | |
_material.color = c; | |
timer = 0; | |
} | |
else | |
{ | |
Color c = _material.color; | |
c.a = 0; | |
_material.color = c; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment