Sprite flicker effect while !canTakeDamage

ezgif-3-0ddb235855

While this might get added later on in the course, I wanted to introduce a visual indicator for when the player is immune to damage. Very simple stuff but also something which feels important for player intuition.

At the start of DamageRecoveryRoutine() I started a new coroutine which swaps between the player’s cached default sprite color and one with a reduced alpha value (iFrameColor), giving the sprite a flickering effect:

    IEnumerator SpriteFlickerRoutine()
    {
        while(!canTakeDamage)
        {
            if(spriteRenderer.color != defaultSpriteColor)
            {
                spriteRenderer.color = defaultSpriteColor;
            }
            else
            {
                spriteRenderer.color = iFrameColor;
            }
            yield return new WaitForSeconds(flickerDelay);
        }
        spriteRenderer.color = defaultSpriteColor;
    }

flickerDelay is a serialized float which I’ve set to 0.1f so that the sprite changes every tenth of a second. When the coroutine is finished I make sure that the color is set to default again.

2 Likes

Great job! This is really awesome, thank you for sharing this with others and myself!

1 Like

Privacy & Terms