Ignore child collider collision

Hi.

I added a Sphere collider to Empty GameObject and set it to child of enemy GameObject.
now I used below code for fire to enemy and reduce health:

public class Gun : MonoBehaviour {

	RaycastHit hit;
    public Camera fpsCam;
    public float range = 100f;
	float Health = 100f
	
	void Update ()
    {	
		if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
		{
			if (hit.transform.name == "Enemy")
            {
				Health -= 10f;
			}
		}
	}
}

but problem is when I shoot to enemy and gun ray collide to Sphere collider, make reduce enemy health. while I want when gun ray collide to enemy body, reduce health.

alt text

anyone know how can fix this problem?

Thanks.

I dont understand 100% your problem but, if you need any collider ignore raycast, set the Layer of that object on Ignore Raycast (inspector).

You can add new Layer and configure what layers have collisions with other layers.

Edit> Project Settings> Tags and Layers

Add your new Layers, in this example, we add new Layer named " GUNLAYER "

Go to the gameObject you want configure her collisions and set layer to " GUNLAYER "

Edit> Project Settings> Physics

Here, we can configure what Layers have collisions with other layers, if you Uncheck the square on GUNLAYER/GUNLAYERS , objects with layer GUNLAYER not have collision with some objects, if you Uncheck GUNLAYER/default, your GUNLAYER objects not have collision with any object with layer default, etc…

Additionaly, if you add the layer reference to your script.

public LayerMask layerGun;

Now, on inspector, you have a new menu, select the GUNLAYER on that menu and modify your raycast code:

if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, layerGun))
        {
            //code
        }

Now, the raycast only detects hits with objects on GUNLAYER layer

Im sorry for my english, I hope this helps you.

Thanks.
it work great;