Ragdoll Issue [GIF + SCRIPTS]
I am looking to test a simple ragdoll script, on all the limbs of the character I have a rigidbody and a capsule collider. When a bullet hits this enemy, it activates a death void which allows the rigidbodies to ragdoll.
I'd imagine this is a pretty straightforward issue since my script is working correctly, I know it has something to do with the way i setup my character. I'm just getting started learning about the possibilities of ragdoll and would love some senior advice!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
SetRigidbodyState(true);
SetColliderState(false);
GetComponent<Animator>().enabled = true;
}
public void Die()
{
GetComponent<Animator>().enabled = false;
SetRigidbodyState(false);
SetColliderState(true);
if (gameObject != null)
{
Destroy(gameObject, 3f);
}
}
void SetRigidbodyState(bool state)
{
Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
foreach (Rigidbody rigidbody in rigidbodies)
{
rigidbody.isKinematic = state;
}
GetComponent<Rigidbody>().isKinematic = !state;
}
void SetColliderState(bool state)
{
Collider[] colliders = GetComponentsInChildren<Collider>();
foreach (Collider collider in colliders)
{
collider.enabled = state;
}
GetComponent<Collider>().enabled = !state;
}
}
Answer by lbryan212 · Jul 17, 2019 at 11:44 PM
Actually I figured this out on my own. For anyone having any similar problems. Make sure you correctly setup your player model with the unity ragdoll wizard. Then if you're going to use this script, attach a collider and rigidbody on the root object of your player. Then you'll be able to switch an animated player from an animation into a ragdoll instantly depending on your use case.