0
\$\begingroup\$

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?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. \$\endgroup\$
    – Kromster
    Aug 26, 2016 at 5:10

1 Answer 1

1
\$\begingroup\$

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)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Or instead of using alpha you can use red tint over your character then apply and remove it back and forth. \$\endgroup\$ Aug 26, 2016 at 6:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .