I am making a game where I am making a health script where I have made a blueprint of what needs to be done. I'm trying a blinking effect after the character is hurt.
Can someone help me make a blink effect in Unity for JS?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI am making a game where I am making a health script where I have made a blueprint of what needs to be done. I'm trying a blinking effect after the character is hurt.
Can someone help me make a blink effect in Unity for JS?
I use a Coroutine to bounce the alpha of the SpriteRenderer back and forth between two values. Unfortunately, this is C#, but is should be really easy to port to javascript.
///Renderer is your sprite's SpriteRenderer
///minAlpha is the lower bound that the alpha will "bounce" between
///maxAlpha is the upper bound that the alpha will "bounce" between
///interval is how long each "bounce" takes
///duration is how long the flashing lasts
public static IEnumerator FlashSprite(SpriteRenderer renderer, float minAlpha, float maxAlpha, float interval, float duration)
{
Color colorNow = renderer.color;
Color minColor = new Color(renderer.color.r, renderer.color.g, renderer.color.b, minAlpha);
Color maxColor = new Color(renderer.color.r, renderer.color.g, renderer.color.b, maxAlpha);
float currentInterval = 0;
while(duration > 0)
{
float tColor = currentInterval / interval;
renderer.color = Color.Lerp(minColor, maxColor, tColor);
currentInterval += Time.deltaTime;
if(currentInterval >= interval)
{
Color temp = minColor;
minColor = maxColor;
maxColor = temp;
currentInterval = currentInterval - interval;
}
duration -= Time.deltaTime;
yield return null;
}
renderer.color = colorNow;
}
You call it like this
StartCoroutine(FlashSprite(renderer, .7f, 1f,2, 3)