Search Unity

Why isnt it possible to ENABLE or DISABLE Rigidbodies anymore ?

Discussion in 'Physics' started by Ekincan, Oct 16, 2019.

  1. Ekincan

    Ekincan

    Joined:
    Sep 8, 2015
    Posts:
    58
    I used to be able to simply disable/enable by toggling this public boolean:
    Rigidbody.detectCollisions

    This used to be very neat because I could disable the rigidbodies while my characters were not in a ragdoll state and the rigidbodies wouldnt affect performance.

    Now it doesnt matter if I toggle this, the performance drain persists and I have to Destroy / Add rigidbodies which is annoying with joints and a more lag spike inducing method than toggling their collision detection was.

    Is there an alternative way to toggle rigidbodies that I dont know of yet (and no I am not going to call the Sleep() method for every rigidbody of every enemy every frame, it doesnt sound like a good solution).
     
    Last edited by a moderator: Oct 17, 2019
  2. Ekincan

    Ekincan

    Joined:
    Sep 8, 2015
    Posts:
    58
    Edit: The detectCollisions variable does still toggle collision detection but it keeps the exact same drain on performance as if you had not toggled it off.
    That wasnt the case in past Unity version.

    Does anyone have a better way than destroying and reinstancing rigidbodies to save performance for ragdollable characters ?
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    There is no other way besides using the .isKinematic boolean. Theoretically it should be "off" if it is both kinematic and not detecting collisions, I'm not sure if it's supposed to still drain performance like that... maybe you could try Destroying/Adding your rigidbodies over time instead of all at once, performing an asynchronous operation like that is a common optimization technique that's saved me countless times.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Increase sleep threshold in physics options. They'll sleep all by themselves sooner then.
     
  5. Ekincan

    Ekincan

    Joined:
    Sep 8, 2015
    Posts:
    58
    Its no use.
    Even with collisionDetection turned off, the rigidbodies sleeping and kinematic they continue draining about the same performance as when active, the sad thing about this is that it wasnt this way in the past.
    Setting them to kinematic and turning collisionDetection off used to completly stop any performance drained by the rigs but thats no longer the case since Unity 2018 or maybe 2017 (I cant exactly remember when it stopped).
    What a shame.

    I will do what I must and what SomeGuy22 already mentioned, destroy and addcomponent the rigidbodies (and disconnect/reconnect them to their joints). I my game characters can turn into ragdolls but also stand up again (if they arent dead yet) so I need to.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I've been using Physx since 2009 ish in Unity and other physcs engines prior, so I'm 100% sure that the problem here is you're doing something - a force - a movement - that is causing them to wake OR it's a bug.

    But it's not a planned behaviour change from Unity so you should be looking at the Physics debugger (it'll show up what's dynamic, what's sleeping etc) and go from there with the assumption that you're causing it. You should be using profiler as well.


    I don't even know the specific unity version you're seeing this "draining" on. You need to be posting a bug report or this will definitely not be resolved for you (there are no reports for it so far). Unity need a bug report from inside the editor so they can reproduce your problem.
     
  7. Ekincan

    Ekincan

    Joined:
    Sep 8, 2015
    Posts:
    58
    I guess you could call this post kind of solved, since I worked around the issue I had.
    My new ragdoll toggling now works by destroying the joints, saving their configuration and after all joints are gone destroying the rigidbodies and it saves all the information so it can reverse that by adding the rigidbodies and after that adding the joints back in (players&enemies dont just ragdoll on death).
    More complicated than I would have liked it but its done and doing the operation async in a coroutine, so I dont get a massive spike so its an acceptable but it annoys me that it used to be much easier.

    I dont need help anymore but if you are interested more on this:
    Game is on Unity 2019.1.10
    The rigidbodies have joints and are connected into a standard humanoid ragdoll and are animated while the enemy is not a ragdoll (in this state setting them to kinematic, turning off collisiondetection and forcing them to sleep didn't change any performance).