Raycast shooting, child collider call and detection
Hi everyone, i'am new with unity and c# programming, this is the first time using raycast; i'm trying to create a simple shooting system with a player emitting raycast and enemies reciving it with childed colliders for different parts of the body. In Hierarchy i have this parent/child structure: an enemy parent (with all his scripts) -> as child: model of the enemy-> as model children: single part of the model, where i would like to put different colliders for each. I've started with this code with which i began to understand how Physics Raycast works; this is the part of code inside the function "Shoot" attached to the player gun that works fine but only with one collider on the parent object
if (Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
if(enemyHealth != null)
{
enemyHealth.TakeDamage(damagePerShot, shootHit.point);
}
gunLine.SetPosition(1, shootHit.point);
}
As you can see, the raycast hit detection is made calling a collider from the script attached to the enemy. The problem is that i don't know how to reference correctly the child colliders in the enemy helath script and then use them for detecting raycast hit inside the shooter script. Any help or advise would be appreciated.