I have a player object and I have attached a sprite to it. I want to create the following effect:
When my player gets hit by an enemy, I want the sprite to blink for a short period of time, like in old arcade games (ex Sonic the Hedgehog).
How can I achieve this effect for my sprite?
Thanks in advance!
@parisk85
Here is the coding method if someone does not like the animation one :-
(in this you will enable and disable the SpriteRenderer component at regular time intervals
create two timers :- (just copy paste them in your code :))
//Sprite blinking effect
public float spriteBlinkingTimer = 0.0f;
public float spriteBlinkingMiniDuration = 0.1f;
public float spriteBlinkingTotalTimer = 0.0f;
public float spriteBlinkingTotalDuration = 1.0f;
public bool startBlinking = false;
void Update()
{
if(startBlinking == true)
{
StartBlinkingEffect();
}
}
void OnTriggerEnter2D(Collider2D col)
{
if(something has entered :) ) //change according to your game
{
startBlinking = true;
}
}
private void SpriteBlinkingEffect()
{
spriteBlinkingTotalTimer += Time.deltaTime;
if(spriteBlinkingTotalTimer >= spriteBlinkingTotalDuration)
{
startBlinking = false;
spriteBlinkingTotalTimer = 0.0f;
this.gameObject.GetComponent<SpriteRenderer> ().enabled = true; // according to
//your sprite
return;
}
spriteBlinkingTimer += Time.deltaTime;
if(spriteBlinkingTimer >= spriteBlinkingMiniDuration)
{
spriteBlinkingTimer = 0.0f;
if (this.gameObject.GetComponent<SpriteRenderer> ().enabled == true) {
this.gameObject.GetComponent<SpriteRenderer> ().enabled = false; //make changes
} else {
this.gameObject.GetComponent<SpriteRenderer> ().enabled = true; //make changes
}
}
}
make changes where i mention and paste the rest of the code as it is, it will work 
animation setbool is not work when collision enemy ?
Hey, Im quit late but maybe for people who will search in future, here:
private IEnumerator Blink() {
SpriteRenderer playerSprite = YourPlayerGameObject.GetComponent<SpriteRenderer>();
Color defaultColor = playerSprite.color;
playerSprite.color = new Color(1, 1, 1,1);
yield return new WaitForSeconds(0.05f);
playerSprite.color = defaultColor ;
}
when calling this function, make sure you do like so:
StartCoroutine(Blink())
and if you wanna give this an arg and call it do it like so:
StartCoroutine("Blink", arg)
MaxEden
6
If anyone still interested there is a small package that does exactly that 