- Home /
How to disable Rigidbody component completely
I am working on a game that have some characters that become ragdolls and after some time, they come back to be controled by animations, the only way I found to do this is setting isKinematic to true in the rigidbodies, which improved the performance compared to leaving isKinematic on false, but even with the isKinematic set to true the performance wasn't even close to when I removed completely the rigidbodies.
What can I do to make the rigidbody be completely ignored by the engine while the character is being controled by the animations?
Should I create the Rigidbodies and the Joints components when I want to activate the ragdoll then remove them when they come back to animations?
Answer by DarthHawk13 · Jul 08, 2016 at 10:10 AM
Here's an example and it works for me. I can turn the rigidbody of an object on and off as I need to.
private Rigidbody rBody; //or public Rigidbody rBody;
void Start()
{
rBody = GetComponent<Rigidbody> ();
}
void Update() //or in any function
{
if (something)
{
rBody.detectCollisions = false;// this will not affect the graphics
// of an object but will make other rigidbodies pass through itt without
//triggering a collision
}
}
Answer by simulism · Sep 15, 2016 at 11:17 AM
I know this is old but you could use:
GameObject.AddComponent<Rigidbody>();
to add a Rigidbody when you need it and then use:
Destroy (rigidbody);
to remove the Rigidbody when not needed.
Answer by samra2494 · Mar 07, 2019 at 07:10 AM
In Unity 4.0+ Rigidbody.active is deprecated and may no longer be available but you can do this:
rigidbody.detectionCollisions = false;
Answer by Fredycom · Aug 12, 2015 at 12:49 PM
Right click on the "Rigidbody component" and I think it might say "Remove Component" or something like that click it.
Answer by GiyomuGames · Aug 12, 2015 at 05:37 AM
Have you tried
GetComponent<RigidBody>().enable = false
?
When I tried this line , I received the error:
error CS1061: Type UnityEngine.Rigidbody' does not contain a definition for
enabled' and no extension method enabled' of type
UnityEngine.Rigidbody' could be found (are you missing a using directive or an assembly reference?)
Ah yeah it doesn't work indeed, sorry I didn't have Unity near me and this works on all components implementing $$anonymous$$onoBehavior (so I guess RigidBody doesn't).
$$anonymous$$aybe you can try calling the RigidBody Sleep method. Not sure what will be the effect to be honest.
GetComponent<RigidBody>().Sleep()
http://docs.unity3d.com/ScriptReference/Rigidbody.Sleep.html