Added blinking when Player is hit by enemy

Added code for blinking when Player is hit by enemy:

In Player class.public void Awake()

// Sprites are added in child game 

objects of main player, 

so iterate through all game objects 

and get required sprite renderers

Transform[] ts = GetComponentsInChildren<Transform>(true);

foreachTransform t in ts)

{

     if (t.gameObject.name == “horse_texturesheet”)

      HorseSpriterenderer = t.gameObject.GetComponent<SpriteRenderer>();

}

In update method we added.

 if (_blinking && (_blinkingTime -= Time.deltaTime) <= 0 )

{

    HorseSpriterenderer.enabled = !HorseSpriterenderer.enabled;

    _blinkingTime = BlinkingTime;

}

// handle takedamage timer

if ( _bHit && (_takeDamageCoolDownTime -= Time.deltaTime) <= 0 )

{

   bHit = false;

   EnableBlinking(false);

   HorseSpriterenderer.enabled = true;

   _takeDamageCoolDownTime = TakeDamageCoolDownTime;

}

And in Take damage we enable the blinking.

public void TakeDamage (int damage, GameObject instigator)

{

   if (_bHit)

      return;

   FloatingText.Show (string.Format (“-{0}, damage), “PlayerTakeDamageText”

       new FromWorldPointTextPositioner (Camera.main, transform.position, 2f60f) );

   Health -= damage;

   if (Health <= 0)

      LevelManager.Instance.KillPlayer();

   // blinking is true, disable player collider for x time

   bHit = true;

   PostCollision(instigator);

   EnableBlinking(true);

}

public void EnableBlinking(bool blink)

{

   _blinking = blink;

}

Thanks for reading.

if you want to check out some of our projects please subscribe to our YouTube channel:

You can check up our work in progress games here:

If you are looking for solutions to game development issues see our blogs here:

If you like our projects please sign up for our newsletter at the bottom of this Page.

Leave a Reply