Whats the best way to activate and deactivate a rigidbody in script
Rigidbody rigidbody = GetComponent<Rigidbody>();
// enable
rigidbody.enabled = true;
// disable
rigidbody.enabled = false;
Unfortunately that does not work for Rigidbodies… they inherit slightly differently.
OP you have to set them to .isKinematic, or else Destroy them and re-add them when you want them back.
So more like:
myRigidbody.isKinematic = true; // disables physics on this object
Ok so i’m using (public Rigidbody rightLeg)
and then (rightLeg.enabled = false)
and it says Rigidbody does not contain a definition for enabled.
Yes.
The problem is when i set them to kinematic my character cant jump anymore.
I guess the best way then would be to create them on death?
Im using it to blow of my robot limbs.
Specifically, Rigidbodies are inherited from Component, whereas the .enabled flag we all love so much is established in Behavior, which also inherits from Component. Rigidbody and Behavior are “peers” in that sense, and the .enabled is established on the Behavior lineage.
Thanks i’ll do some research on that later.
God bless you all.
Oh wow I spoke too soon! TIL. Thanks for the correction Kurt.
i am sorry for reviving resolved discussion. this is for future reader.
if your goal is to make the rigidbody to be out of physics simulation,
i think the best way is to set myRigidbody.simulated = false;
i think this is better than setting isKinematic = true;
because kinematic rigidbody is still calculated by physics simulation.
The simulated
property is only available on Rigidbody2D though, not the 3D version.
Actually…
myRigidbody.isKinematic = true;
is a bad way of doing things because you get an error saying that there’s an unity upgraded version where you do:
GetComponent<Rigidbody>().isKinematic = true;
So that’s probably the best way for anyone reading this in the future.
Sorry if that came off to be a bit rude to anyone who was saying to do: myRigidbody.isKinematic = true;
Anyways that at least is just what works for me.
Edit: It is a bit similar though.
This is incorrect. You only get that error if you use the built-in property called exactly “rigidbody”.
Or more likely, myRigidbody can be the name of a variable containing the reference to the rigidbody…
This does not compile, LOL.
As noted in my post #3 above AND discussed further in my post #6 above. Do you have an actual question?
Thank you so much!!! I’ve been stuck for an entire week on this! It works!
thanks for the help
As mentioned above, the simulated
property isn’t available on a regular Rigidbody (only a Rigidbody2D) - but equivalent functionality is to set detectCollisions
to false via: myRigidbody.detectCollisions = false;
I’m using this to prevent collisions when fading out an object after its lifetime has expired, so I also set myRigidbody.useGravity = false;
otherwise the object starts to fall through the floor as soon as I disable collisions.
This is exactly what I needed. Thanks!
I think that’s enough necroing for one thread.
Please use the like button rather than necroing threads please.
Thanks.