Skip to content

Instantly share code, notes, and snippets.

@FleshMobProductions
Last active May 27, 2018 03:28
Show Gist options
  • Save FleshMobProductions/0265ff134fa9a1bb3acb31bea6a9a43d to your computer and use it in GitHub Desktop.
Save FleshMobProductions/0265ff134fa9a1bb3acb31bea6a9a43d to your computer and use it in GitHub Desktop.
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();
}
}
}
}
@jasonleekennedy
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment