Search Unity

Question Performant way to handle ragdoll switching

Discussion in 'Scripting' started by calpolican, Oct 14, 2021.

  1. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    I worked in this script that allows me to switch a character from ragdoll to animated. I worked in adapting the logic to my game and to make it compatible with the IA and it works fine now. But I want to optimize it a bit.
    I may have to switch back and forth to ragdoll mode at any point during gameplay, so I decided to use a hybrid skeleton instead swaping the skeletal mesh for an instantiated ragdoll.
    Since the limbs can become ragdolled, they now have a collider, a rigidbody and a joint. This 3 components are there the whole time, even if the character is not in ragdoll mode. I'm wondering how to manage this to make it efficient.
    My idea is to just disable these 3 components when the character is animated. The first thing I noted is that rigidbodies can't be disabled, and so I'm only seting them as kinematic. I'm not sure if this is really the best way, though. I'm wondering if instead I should destroy them and recreate them when necesary. Not sure what it may be doing when set to kinematic, or what may be the cost.
    Joints have a very similar problem, as they can't be disabled.
    Also, I'm wondering if disabled colliders may be adding up, even when disabled, as I have detected some weird behaviour when raycasting form the camera to their position.
    Did anyone deal with this? Can you talk about your findings?
     
    Last edited: Oct 14, 2021