- Home /
My player HP are get decreased from several GameObjects that has is Trigger on
So my enemy got 2 children's objects one is named "Claw" that has circle collider 2D and the other is "Attack check" that has circle collider 2d too so it can check if my player is close to attack him, both those objects got is trigger on of course. Then i decide to make enemy decrease my player health by attacking it i created emptyobject "HealthController" so it could show me my HP in canvas text then i create to new scripts "HealthController" & "DamageController"
i gave "DamageController" script to child "Claw" and gave him 20 dmg to deal my player. this is "HealthController" script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : MonoBehaviour
{
public float KilluaHealth;
[SerializeField] private Text healthText;
private void Start()
{
UpdateHealth();
}
public void UpdateHealth()
{
healthText.text = KilluaHealth.ToString("0");
}
}
and this is the "DamageController" script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageController : MonoBehaviour
{
[SerializeField] private float ClawDamage;
[SerializeField] private HealthController healthController;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Killua"))
{
Damage();
}
}
void Damage()
{
healthController.KilluaHealth = healthController.KilluaHealth - ClawDamage;
healthController.UpdateHealth();
this.gameObject.SetActive(false);
}
}
in the end i gave my player 100 hp, and gave "Claw Damage" 20 but when i collide with enemy my hp become from 100 to 60 because it's count other objects that has trigger on. who knows solution to fix this?
I'm apologize if my English was bad it's not my main languge
no i colliding once but because child objects got is trigger on too somehow it's counting for attack too
Unless the child object also has the script on, the collision will not be counted twice.
Your answer
Follow this Question
Related Questions
Take health from enemy 3 Answers
How to call OnTriggerEnter once 5 Answers
Respawn from falling off world! 6 Answers
Trigger collision with multiple objects suddenly becomes unreliable. 1 Answer