Hello, I am trying to create a Heart System for a 2D platform game but I am missing on how to create the Invulnerability for a couple of seconds or frames after getting hit. I have done this before but for Health Bars, not this system, my script so far goes like this for the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthPlayer : MonoBehaviour
{
public int health;
public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
public float timeInvincible = 10f;
bool isInvincible;
float invincibleTimer;
private void Update()
{
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
{
hearts*.sprite = fullHeart;*
}
else
{
hearts*.sprite = emptyHeart;*
}
if (i < numOfHearts)
{
hearts*.enabled = true;*
}
else
{
hearts*.enabled = false;*
}
}
}
public GameObject deathEffect;
public void TakeDamage(int damage)
{
{
if (!isInvincible) { health -= damage; }
}
if (health <= 0)
{
Die();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
Time.timeScale = 0f;
}
}