1

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
            }


        }
3
  • Just a wild guess, should it be "yield return new WaitForSeconds(3);"?
    – rs232
    Nov 15, 2018 at 11:54
  • But then i need a coroutine and since it is only a bool i have to make it do something so the player cant be damaged somehow but i guess since i make the function not usable it will be invicible or? So maybe ill try a coroutine :D Nov 15, 2018 at 11:56
  • How it is now, it will do the following: 1) make the player invincible. 2) immediately create an object which represents (just represents, it does not do anything on its own!) waiting for three seconds. 3) immediately make the player able to receive damage again. 4) proceed with other tasks
    – rs232
    Nov 15, 2018 at 12:15

1 Answer 1

1

So I just made a simple collision 'game' using the method you provided and everything works fine. But now as I look further, I am seeing some redundancy in what you provided and your 'answer' is a bit confusing.

But your first script shows that every time the damage dealer collides with 'Player' it does damage.

private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("Player"))
            {
                HealthBarScript.health -= 20f;
            }

        }

AND every time the 'Player' collides with an 'enemy' it does an invincible check which should be working. (Assuming the comments are removed). But as the comments stand there is no damage from the 'Player' hitting the damage structure but always (regardless of invincible) damage from the structure to the player.

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
            StartCoroutine(Invincible());                   // makes this whole function reusable since invincible is false again
        }


    }
}


IEnumerator Invincible()
{
    if (invincible == true)


    {
        yield return new WaitForSeconds(2);
        {


            invincible = false;
        }
    }

But when you hit again before the invincible check expires the script on the enemy is still dealing damage.

You have two scripts doing damage and only one does an invincible check.

Get rid of the damaging script on the structures, tag all the structures properly, comment the script on the player and you will be good to go. Or you can keep the script on the damage structures and just have each one check if the player is invincible by making that bool static.

Also you could have easily answered your own question with standard usage of Debug.Log("what the heck is happening"). Debug.Log should be your best friend.

2
  • okay i will try but how would u go about only getting 20% of the health taken and not 60% if it hits 3 objects? Nov 15, 2018 at 14:59
  • I made it work had tagged enemy when i needed Enemy but now i just need to be sure it can only hit for 20% = 1 heart he has 5 hearts displayed so it takes them out on each life he looses :D Nov 15, 2018 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.