Last active
May 27, 2018 03:28
-
-
Save FleshMobProductions/0265ff134fa9a1bb3acb31bea6a9a43d 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/// <summary> | |
/// Attach the script to a component with a spriterenderer. | |
/// Set the desired color for the damage state with SetDamageColor(Color color) | |
/// Call EnableDamageEffect(float duration) to activate the damage effect for a desired duration | |
/// Call DisableDamageEffect() to disable the effect immediately | |
/// </summary> | |
[RequireComponent(typeof(SpriteRenderer))] | |
public class DamageEffect : MonoBehaviour | |
{ | |
public Color damageColor; | |
/// <summary> | |
/// How long does it take to switch from default color to the damage color | |
/// </summary> | |
public float flickeringDuration; | |
/// <summary> | |
/// If smooth transition is disabled, the color will switch immediately from damage to regular without lerping inbetween | |
/// </summary> | |
public bool enableSmoothTransition; | |
private Color defaultColor; | |
private Color currentColor; | |
private new SpriteRenderer renderer; | |
private bool isDamageEffectActive; | |
private float effectTimeFull; | |
private float currentEffectTime; | |
public void SetDamageColor(Color color) | |
{ | |
this.damageColor = color; | |
} | |
public void SetSpriteRenderer(SpriteRenderer renderer) | |
{ | |
this.renderer = renderer; | |
defaultColor = renderer.color; | |
currentColor = renderer.color; | |
} | |
public void SetDefaultColor(Color color) | |
{ | |
defaultColor = color; | |
} | |
void Awake() | |
{ | |
isDamageEffectActive = false; | |
effectTimeFull = 0f; | |
currentEffectTime = 0f; | |
var renderComponent = GetComponent<SpriteRenderer>(); | |
SetSpriteRenderer(renderComponent); | |
} | |
public void EnableDamageEffect(float duration) | |
{ | |
isDamageEffectActive = true; | |
effectTimeFull = duration; | |
currentEffectTime = 0f; | |
} | |
public void DisableDamageEffect() | |
{ | |
isDamageEffectActive = false; | |
effectTimeFull = 0f; | |
currentEffectTime = 0f; | |
ResetVisualDamage(); | |
} | |
private void ResetVisualDamage() | |
{ | |
renderer.color = defaultColor; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (isDamageEffectActive) | |
{ | |
currentEffectTime += Time.deltaTime; | |
if (enableSmoothTransition) | |
{ | |
float durationRemainder = Mathf.PingPong(currentEffectTime, flickeringDuration); | |
float lerpVal = durationRemainder / flickeringDuration; | |
renderer.color = Color.Lerp(defaultColor, damageColor, lerpVal); | |
} | |
else | |
{ | |
int divisonResult = (int)(currentEffectTime / flickeringDuration); | |
if (divisonResult % 2 == 0) | |
{ | |
renderer.color = defaultColor; | |
} | |
else | |
{ | |
renderer.color = damageColor; | |
} | |
} | |
if (currentEffectTime >= effectTimeFull) | |
{ | |
DisableDamageEffect(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would consider doing the damage effect as a co-routine that way you don't need to eat the cost of this being in the update loop when the damage effect isn't happening.