2

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;
     }```
  • Probably the answer to your question is here. https://gamedev.stackexchange.com/questions/151670/unity-how-to-detect-collision-occuring-on-child-object-from-a-parent-script – Adamer Feb 06 '22 at 09:26

1 Answers1

0

Why not [SerialisedField] protected RigidBody headHitChhildObject, ribHitChhildObject;

Drag and drop to inspector. Than in start function. headHitChhildObject = findObjectOftType ().gameObject.GetComponent() ribHitChhildObject= findObjectOfType (). gameObject.GetComponent () (Do this seperate for each object)

Change take damage parameters to Public Void TakeDamage (Collider triggerObject) --- I would use a float personally for damage/health, but that's just me. In on trigger function. TakeDamage(collision)

Inside Take Damage Fumction have the following lines For if (triggerObject.attachedRigidBody ! = null && siblings.name = = putname) currentHealth - = damage amount For the Else If(triggerObject. attachedRigidBody ! = null && siblings.name = = putOtherName) currentHealth - = damage amount

(My more optimal suggestion) Or better yet, have a Master script for handling the hits.(listener) Pretty much take damage and numerator. public Void On Enable() MyGameEvents.onTriggerColliderEvent +=
TakeDamage ;

public Void On Disable()

MyGameEvents.onTriggerColliderEvent -= TakeDamage ;

a delegation script for handling events across scripts(not just these scripts, but other scripts, instead of constant cross referencing multiple times and long code!

public static class MyGameEvents or something you chose

public delegate void OnTriggerCollider(Collider triggerObject, int amount) ;

Public static OnTriggerCollider onTriggerColliderEvent ;

In a script on each trigger children components just have; [SerialisedField] protected int damageToCall // Set in the inspector, so you can experiment with this)

OnTriggerEnter(Collider collider)
MyGameEvents.onTriggerColliderEvwnr?.Invoke(Collider, damageToCall)

and you can use the public static MyGameEvents for handling and coding that is reoccurring across other scripts. Such as camera shake, ground shake, body part shake, health regen when certain req's are met. Power Up Events, object destroy on Zero health events. Score points, etc.

Think of it as a way more reusable and versatile unity events all the YouTube dev wannabes don't use properly.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Neeraj Aug 24 '22 at 05:14