Hey I want to make my player invicible, when he takes damage and loose a health. ATM I cant make my player invicible, and also if he collides with several damage structures he takes more damage than intended I want him to lose 20% of his health and then be invincible for 2-3 sec.
This is attached to my damage structure
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoseHealth : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
HealthBarScript.health -= 20f;
}
}
}
This is for my FullHearts on the canvas attached as a child to my "empty hearts"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthBarScript : MonoBehaviour {
Image FullHearts;
float maxHealth = 100f;
public static float health;
// Use this for initialization
void Start () {
FullHearts = GetComponent<Image>();
health = maxHealth;
}
// Update is called once per frame
void Update () {
FullHearts.fillAmount = health / maxHealth;
if (health == 0)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
ATM I have this on my playerscript which is not working all other scripts are working but just need it to only be 1 heart to lose no matter if the player hits multiple structures and then invicible to have time to recover and not hit the structure :
if (!invincible)
{
if (col.gameObject.tag == "enemy")
{
// HealthBarScript.health -= 20f;
// health -= 20; // subtract 1 form your total health
invincible = true; // makes this whole function unusable since invincible is no longer false
new WaitForSeconds(3);
invincible = false; // makes this whole function reusable since invincible is false again
}
}