Skip to content

Instantly share code, notes, and snippets.

@NickDiMucci
Last active December 14, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickDiMucci/5022335 to your computer and use it in GitHub Desktop.
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.
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