Is there anyway to check collision beside the OnCollision type Function?

Let say that I have a GameObject which is a melee weapon.

The part that is use for hit detection is a Child Transform.
There could be several per Weapon

The Main Script is attach only to the Base Transform.
Main Script has reference to all of the colliders in Children Transform.

Even If more than one Collider hit an enemy,
DealDamage() function should be call only once.

Instead of calling OnCollision from the Child Transform,
Is it possible to detect if any of the collider in children hit something instead?

1 Answer

1

You could create a simple cool down function, which enabled or disables Boolean. When there is a collision run something similar:

bool canAttack = true;

if(canAttack){
    if(There is collision){
        StartCoroutine(Attack());
    }
}

IEnumerator Attack(){
    canAttack = false; //cant attack no more
    DealDamage();
    yield return new WaitForSeconds(1); //waits 1 second
    canAttack = true; //can attack after 1 second
}

Thank you YoungDeveloper, you had solve half of my issue. The other one was that the Collider and Script weren't attach to the same Transform.

Thank you for your help, currently we have the 2nd option implemented, when what we wanted to achieve was the 1st option. The problem was that we couldn't figure out the function that check whether any of the child collided with something or not. We found OnCollisionEnter and OnTriggerEnter, but we couldn't get them called from the parent object

Again, Thank you for your help, I will try it out.

Thank you, I got it working now! As a sidenote, it will not work if the child also contain RigidBody