What is the best way to figure out which body part was hit on a rigged character?

I have a rigged character imported from blender which I'm animating. I want to shoot it, and know which bit I shot (head, body, arm, etc), so I can have it react appropriately. I shoot it by using Physics.RayCast, which works fine and reports a collision with the CharacterController which is attached to the character. This doesn't let me be any more precise with the collision though.

It seems like the solution to this is to put individual colliders on each body part (like in a ragdoll). Is there a performance cost associated with this?

Is there an alternative way of accurately determining hit location rather than using lots of colliders?

My method is to have a primitive collider on the bones. Works great with animations.

You do not have to have damage reciver script or rigidbody on each bone, in fact you do not need to have rigidbody at all (Unless you want to apply physics).

As newer versions of Unity will treat all child colliders as a compound collider. Inside the raycast you can get the name of the body part (bone) that was hit through hit.collider.name and use that to apply the correct amount of damage, or apply force.

Here is a quick theory that you can try until someone gives a better answer. In short its the same as how you detect character damage but your dropping the script on the individual body parts.

Add in the script to print out "Hit BodyPartHere" whenever that part is being used to deduce the damage from the main character damage script.

Check the console after you have shot that body part to verify, and if it works let me know! Im aiming to accomplish the same thing but I have other tasks on my plate right now. If I get to it I will confirm this theory works and drop the code in.

Well the only alternative to accurately determine hit location without using lots of colliders is… using less colliders :smiley:

Seriously: the performance cost is fortunately trivial. For example, if you can have 200 game characters with 25 bones in scene at 170 fps, if you put kinematic colliders and rigidbodies in them you need to go down to 180 game characters to reach 170 fps again.

But if performance really matters in your case, you can use three colliders, for example. One box for the spine and legs and head, and one for each arm, and use ‘closestpointonbounds’ after you raycast the colliders.