- Home /
Detach Arm Body Part
So I have a robot enemy and when I shoot It with my pistol I want the game object that got shot to fall off and act as a ragdoll (kinda like in Danis Karlson game). I tried putting a script on every body part of the game object that unparents but it doesn't unparent. So my model looks exactly like Danis robot model for the game Karlson (not expecting anyone to know what that is but just in case) and when I shoot it with a bullet (not raycasted using addforce) and all its health runs out the last body part that was shot falls off. The model has bones and is using a navmeshagent and has a walking and idle animation. I am going to try to add a muti aim constraint. https://www.youtube.com/watch?v=yqmiuPF9YEU&t=161s This is the devlog of Karlson where he makes the robot and also you will see at 2:22 in the video his body parts fall off when out of health. Animations were made in unity.
Thanks any help appreciated.
Answer by Darkmatter520 · Jun 25, 2020 at 03:26 PM
You may need to detach the physics joints themselves! I believe that the parenting hierarchy does not effect physically attached rigid-bodies like a ragdoll has, it is more for organization.
So you will need to first Unparent, then access the physics joint (I think it is called a Character Joint) and disable it, or remove the component.
I am assuming your character has individual arms and legs, not all one skinned mesh right? In that case it would get slightly more complicated.
Hi, yeah so everything is separated not all one skinned mesh.
Answer by tuinal · Jun 25, 2020 at 03:51 PM
If it's an animated unrigged character:
Add a script per part with a callback to a base script on the root. This is easier than trying to manage/store part lists centrally & can be easily extended to give different hp/armor to different parts, or make certain parts nondetachable simply by not attaching the script, e.g.
public class BotPart : MonoBehaviour
{
BotAI controller;
private void Awake()
{
controller = GetComponentInParent<BotAI>();
}
public void TakeHit()
{
controller.PartHit(gameObject);
}
}
However, it's usually better to manage most of the behaviour centrally, because you'll have a single animator and usually need to adjust the AI and animation behaviour if, for example, a leg is missing. So in a single BotAI script on the root;
public void PartHit(GameObject part)
{
Destroy(part.GetComponent<ConfigurableJoint>()); //remove appropriate joint to stop the physical joint
part.transform.parent = null; //clear parent to stop the animation
anim.Rebind(); //where anim is the animator. This ensures detached bits stop animating. Clearing parent alone may not work.
part.GetComponent<RigidBody>().isKinematic = false; //rb should be set to kinematic when animating. This reverses that meaning it will fall. You may want to do this recursively foreach child of the detaching object.
}
When your gun raycasts, do
if(hit.collider.GetComponent<BotPart>!=null) {hit.collider.GetComponent<BotPart>().TakeHit();}
As the poster above notes if it's a SkinnedMesh (e.g a zombie) this is considerably more complicated; the only way to 'detach' a part of a skinned mesh is to set it's bone scale to 0, and spawn a new separate mesh for the bit that's blown off.
Ok, so I am extremely confused by this, I think I should explain myself a bit better, I am going to update my question.