Search Unity

Resolved Deactivate rigidbody

Discussion in 'Scripting' started by GodsKnight117, May 14, 2020.

Thread Status:
Not open for further replies.
  1. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
    Whats the best way to activate and deactivate a rigidbody in script
     
    Sammyueru1 likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Code (CSharp):
    1. Rigidbody rigidbody = GetComponent<Rigidbody>();
    2.  
    3. // enable
    4. rigidbody.enabled = true;
    5.  
    6. // disable
    7. rigidbody.enabled = false;
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    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:

    Code (csharp):
    1. myRigidbody.isKinematic = true;  // disables physics on this object
     
  4. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
    Ok so i'm using (public Rigidbody rightLeg)

    and then (rightLeg.enabled = false)

    and it says Rigidbody does not contain a definition for enabled.
     
  5. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
    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.
     
    AlejandroHaibi likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    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.
     
  7. GodsKnight117

    GodsKnight117

    Joined:
    Mar 13, 2018
    Posts:
    43
    Thanks i'll do some research on that later.
    God bless you all.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Oh wow I spoke too soon! TIL. Thanks for the correction Kurt.
     
    Kurt-Dekker likes this.
  9. MikhaelMartin

    MikhaelMartin

    Joined:
    Sep 4, 2019
    Posts:
    7
    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.
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    The
    simulated
    property is only available on Rigidbody2D though, not the 3D version.
     
  11. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    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.
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    This is incorrect. You only get that error if you use the built-in property called exactly "rigidbody".
     
    Bunny83 likes this.
  13. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Or more likely, myRigidbody can be the name of a variable containing the reference to the rigidbody...
     
  14. unity_FHakNWfekmd7Pg

    unity_FHakNWfekmd7Pg

    Joined:
    Nov 26, 2020
    Posts:
    1
    This does not compile, LOL.
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    As noted in my post #3 above AND discussed further in my post #6 above. Do you have an actual question?
     
    Bunny83 likes this.
  16. jkhenfer

    jkhenfer

    Joined:
    May 13, 2021
    Posts:
    1
    Thank you so much!!! I've been stuck for an entire week on this! It works!
     
    Last edited: Jun 15, 2021
    AlMartson likes this.
  17. colemandaley7

    colemandaley7

    Joined:
    Nov 9, 2022
    Posts:
    1
    thanks for the help
     
  18. r3dux

    r3dux

    Joined:
    Aug 8, 2018
    Posts:
    2
    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.
     
  19. drewnusser

    drewnusser

    Joined:
    Aug 18, 2013
    Posts:
    13
    This is exactly what I needed. Thanks!
     
    titch_stewart likes this.
  20. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    I think that's enough necroing for one thread. :)

    Please use the like button rather than necroing threads please.

    Thanks.
     
Thread Status:
Not open for further replies.