I'm trying to make a script that allows the head hitbox, and the chest hitbox which are children to the main game object, to be detected by the main script so that damage can take place. For example, the main script knows when the head collider is hit, vs the body collider. (Currently, doesn't work even if enabled). I've tried many scripts and searched for answers and I really can't find them. Please help. Note, the comments as far as I know have little to nothing to do with the issue I'm having. (Edit): Clarified which part of the script I'm having trouble with.
Here's the part of the script that is giving me trouble:
public void OnTriggerEnter(Collider collider)
{
myLastHit = collider.gameObject.GetComponent<PunchDetection>().punched;
foreach(Transform sibling in transform.parent)
{
if(sibling.name == "HeadHitbox")
{
collider.enabled = true;
//Collider col = child.GetComponent<SphereCollider>();
if (canTakeDamage && myLastHit is SphereCollider)
{
TakeDamage(15);
StartCoroutine(damageTimer());
}
}
if (sibling.name == "RibsHitbox")
{
collider.enabled = true;
//Collider col = child.GetComponent<CapsuleCollider>();
if (canTakeDamage && myLastHit is CapsuleCollider)
{
TakeDamage(5);
StartCoroutine(damageTimer());
}
}
}
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
private IEnumerator damageTimer()
{
canTakeDamage = false;
yield return new WaitForSeconds(damageTimeout);
canTakeDamage = true;
}```