Saturday, October 22, 2016

Handling exact collisions on character controller and/or other objects in unity? (eg. head shot, chest , leg,etc)


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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...