Answers for "how to detect damage in unity"

C#
1

how to detect damage in unity

public int currentHP;
    public GameObject Enemy; // These are to find the GameObjects
    public GameObject Player;
    
    void Start()
    {
        (Enemy) = GameObject.Find("Enemy"); // Set this to the GameObjects name
        (Player) = GameObject.Find("Player"); // Set this to the GameObjects name
    }
    void Update()
    {
            if (currentHP < 1) // This is to destroy the player when you reach 0 HP
        { 
            Destroy(Player); // You can change this to a different method
        }      
    }
    private void OnCollisionEnter2D(Collision2D collision) // Detecting the damage
    {
        if (Enemy)
            currentHP =-1;
    }  
// You can add a health cap if you need to
Posted by: Guest on February-23-2021

Code answers related to "how to detect damage in unity"

C# Answers by Framework

Browse Popular Code Answers by Language