Headshot Collision

Basically I’ve got a zombie that only is effected by headshots, however when I have a bullet or ray or w/e collide with the object it pulls the parents tag not the tag from the head, (I have a collision attached to the head bone I want to be able to tell via tag if this is the part hit) how do I get it to pull the tag of the specific bone/body part it has hit?

3 Answers

3

I am not sure how one would achieve what you are trying to do in that exact way, but let me provide a close enough alternative:

You could attach a gameObject with a collider to the head portion of the mesh, where the collider is a close enough fit. Then you could assign the desired tag to the “collider gameObject” and should be able to perform the head shot detection on that.

I presume you would want to have multiple zombies, so you could create a prefab for this, meaning you would only have to set it up once and instantiate the prefab each time a zombie is created.

Hope this helps.

Cheers

Bilo

Ok Well I did that, same problem happeneds, it just goes to the highest up on the parent list, I don't know why.

Ok so I actually know this one since I’ve had some frustrating moments with it in my own project.

If the parent is a rigidbody and the children are all exclusively colliders, it will act as a compound collider. You can look that up in the documentation. It basically just means a rigidbody comprised of multiple colliders.

Either way, if you were to cast a ray to any of the colliders within the compound collider, RayCastHit hit would give you the parent (the one with the rigidbody). If you want to get the individual colliders you’d do hit.collider.

I hope this helps at least somewhat.

Thanks. That really helped me! Almost 5 years on.

After some research, I’ve found an easy way to do this without any workarounds.

When creating Colliders, use different names in each of them, as head, torso, legs, arms.
Then on OnCollisionEnter method, you just had to check all of the contact points.

void OnCollisionEnter(Collision col)
{
	foreach (ContactPoint contact in col.contacts)
	{
		Debug.Log(contact.thisCollider.name + " hit " + contact.otherCollider.name);

		if (contact.thisCollider.name == "HitBoxHead") {
			Debug.Log("Head Shot");
		}
	}
}