0
\$\begingroup\$

Hello all I have been working on games in Unity for a long while (making slow progress as I do it purely on my own , and of course you helpful people here on gamedev.stackexchange!)

I've made lots of capsule characters of my own using Rigidbody and without Rigidbody and on a basic level they work fine. But i now want to detect exact points of collisions (for example if a bullet hits his head, or hand or knee etc etc)

I've been trying to read as much as I can find about this, but honestly can't find the answer i need. I believe I would achieve it by making several colliders on the character. But I don't know exactly how this would work , also I have seen some guides saying that I should use OverlapSphere but again there wasn't exact instructions and I can't figure out how to do it.

Also, when I add some sphere colliders to my character, they dont stay in the correct position in relation to the model once it is animated (eg. Crouching, or running, the leg spheres pop out of his legs for example).

Could anyone here provide an explanation of how I can do it?

\$\endgroup\$
2
\$\begingroup\$

Attach your body part colliders to the transforms representing the animated character's bones.

Set each one's "Is Trigger" property to true, so you can use them for checking collisions with projectiles, without altering the movement or physics simulation of your characters and their surroundings.

Add a Rigidbody component to each one, set to Kinematic, so they're driven solely by the animation of those bones.

Set them up on a new physics layer that doesn't collide with your character controller's capsule or the environment, only with projectiles / weapon raycasts, to reduce redundant collision checks & messages.

You can then check for hits on these body part triggers in a number of ways:

  • Fire a raycast (or spherecast, etc) masked to the environment & body part layers. This is useful for instantaneous effects like a laser, or high-speed bullets that don't take an appreciable amount of time to travel from the barrel.

  • Fire projectile Rigidbodies and detect collisions with an OnTriggerEnter method, on either the body part or the projectile object. This is good for low-speed or lobbed projectiles. You can also do this with persistent objects to represent things like tripwires or energy fields the player could walk into.

  • Use volume queries like OverlapSphere to check for body parts within an area of effect. This is good for instantaneous blasts (like finding everything in the radius of a grenade at the moment of detonation)

Make sure to pass the argument QueryTriggerInteraction.Collide to ensure your raycasts/volume queries don't ignore triggers.

\$\endgroup\$
  • \$\begingroup\$ thank you so much for this answer. I ran out of time to work on the games for the last couple of days but I will try to put this all into practise very soon. thanks again \$\endgroup\$ – Big T Larrity Sep 7 '18 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.