Hello Everyone,
I’m having problems with my Heal Object, I dont know if the problem is with the collusion of player and object or if there’s something wrong in the code for it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealSystem : MonoBehaviour
{
public float QuantidadeCura;
private void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("Player") && other.GetComponent<HealthController>())
{
other.GetComponent<HealthController>().HealthGain(QuantidadeCura);
}
}
}
And the health code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : MonoBehaviour
{
public float health = 14;
public float Health
{
get { return health; }
set { health = Mathf.Clamp(value, 0, healthMax); }
}
public float healthMax = 14;
public float healthDecreaseRate = 0.1f; // Taxa de diminuição da vida por segundo
public Image healthBar;
public GameObject enemyObject; // Referência para o objeto do inimigo
public GameObject Clone; //Referência Clone
private void Start()
{
enemyObject.SetActive(false); // Desativar o objeto do inimigo no início do jogo
Clone.SetActive(false);
}
private void Update()
{
// Diminuir a vida com o tempo
Health -= healthDecreaseRate * Time.deltaTime;
UpdateHealthBar();
// Verificar se a vida está abaixo de 10 e o inimigo está desativado
if (Health < 10 && !enemyObject.activeSelf)
{
ActivateEnemy();
}
//Ativar "clone"
if (Health < 13 && !Clone.activeSelf)
{
ActivateClone();
}
}
private void UpdateHealthBar()
{
healthBar.fillAmount = Health / healthMax;
}
private void ActivateEnemy()
{
enemyObject.SetActive(true); // Ativar o objeto inimigo
}
private void ActivateClone()
{
Clone.SetActive(true); // Ativar o objeto Clone
}
public void HealthGain(float Cura){
Debug.Log("Cura");
Health += Cura;
}
}