- Home /
How to make is kinematic false for all children of a gameobject?
I have an enemy fully rigged, with each child being a certain bone. I am trying to make it so that once you shoot the enemy with a raycast, (which I have already programmed) it makes "is kinematic" false for every ridgid body of each child/bone of the enemy.
Here is my code so far. I was planning on setting is kinematic to false under "void die," but I wasn't sure on how to do it.
public GameObject enemy;
public float health = 1;
public void takeDamage(float amount)
{
health -= amount;
if (health <= 0f)
{
die();
}
}
void die()
{
}
Answer by AntonioF · May 19, 2018 at 06:04 AM
void SetIsKinematicWithGetComponents(GameObject obj, bool foo)
{
Rigidbody[] rigidbodiesOfAllChild = obj.GetComponentsInChildren<Rigidbody>();
for (int i = 0; i < rigidbodiesOfAllChild.Length; i++)
{
rigidbodiesOfAllChild[i].isKinematic = foo;
}
}
void SetIs$$anonymous$$inematic ( GameObject go , bool is$$anonymous$$inematic )
{
foreach( var rb in go.GetComponentsInChildren<Rigidbody>() ) rb.is$$anonymous$$inematic = is$$anonymous$$inematic;
}
It's shorter this way. GetComponentsInChildren starts with gameObject and not children so calling GetComponents too is redundant.
Thank you so much! I've been trying to get this to work for so long and it worked.
Your answer
Follow this Question
Related Questions
How to effect all objects in an array 2 Answers
Death and animations 1 Answer
G-Mod like physics that kill? 1 Answer
Ragdoll get hit by 2 Answers
Ragdoll Death Script 1 Answer