Last active
December 14, 2015 03:39
-
-
Save NickDiMucci/5022335 to your computer and use it in GitHub Desktop.
How I'm creating a color flash effect on the player when he gets hit by an object.
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
public Color[] flashColors; | |
private float flashLerpDuration = 0.10f; | |
private float flashInvokeDuration = 0.5f; | |
void Start() { | |
// All other initialization code here... | |
flashColors = new Color[2]; | |
flashColors[0] = Color.magenta; | |
flashColors[1] = Color.white; | |
} | |
void Update() { | |
// All other update code here... | |
if (IsInvoking("colorFlash")) { | |
checkColorFlashInvoking(); | |
} | |
} | |
void OnTriggerEnter(Collider other) { | |
// All other on trigger enter code here... | |
if (checkForCollisions(other)) { | |
InvokeRepeating("colorFlash", 0, 0.10f); | |
} | |
} | |
private void colorFlash() { | |
float lerp = Mathf.PingPong(Time.time, flashLerpDuration) / flashLerpDuration; | |
renderer.material.color = Color.Lerp(flashColors[0], flashColors[1], lerp); | |
} | |
private void checkColorFlashInvoking() { | |
flashInvokeDuration -= Time.deltaTime; | |
if (flashInvokeDuration <= 0) { | |
CancelInvoke("colorFlash"); | |
flashInvokeDuration = 0.5f; | |
renderer.material.color = Color.blue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment