2D Platformer: Health (Heart UI)

Changed the text based health system, to a heart sprite.
2 player health = 1 heart.
Used variables of fullHeart, halfHeart, and emptyHeart.
Once these values are detemined from the player’s currentHealth, and maxHealth, the heart prefabs are instantiated into a list and then displayed on the Canvas’ Grid Layout Group. Piece of the code I decided to go with.

    public void CreateHearts()
    {
        ClearHearts();

        float fullHearts = MathF.Floor(PlayerHealth.instance.health / 2);
        float emptyHearts = MathF.Floor((PlayerHealth.instance.maxHealth - PlayerHealth.instance.health) / 2);

        for (int i = 0; i < fullHearts; i++)
        {
            InstantiateHeart("Full");
        }
        if (PlayerHealth.instance.health % 2 != 0)
        {
            InstantiateHeart("Half");
        }
        for (int i = 0; i < emptyHearts; i++)
        {
            InstantiateHeart("Empty");
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *