Would there be a way to set up a ragdoll on unit itself instead? So we wouldnt have to set up an individual unit and individual unitfagdoll for every unit we add to the game? One on the unit itself and we could just enable it on death?
1 Like
You can, but it requires some extra setup.
Once you have ran the Ragdoll Wizard on the character, you’ll need to tag each GameObject in the skeleton that has a Ragdoll collider or Rigidbody with the tag Ragdoll (You can create any tag you want for this, but making one called Ragdoll is clear).
Then you’ll need to add this script to the character:
Ragdoll.cs
using System.Linq;
using UnityEngine;
public class Ragdoll : MonoBehaviour
{
[SerializeField] private Animator animator;
private Collider[] allColliders;
private Rigidbody[] allRigidbodies;
private bool isRagdoll = false;
void Start()
{
allColliders = GetComponentsInChildren<Collider>(true).Where(c => c.gameObject.CompareTag("Ragdoll"))
.ToArray();
allRigidbodies = GetComponentsInChildren<Rigidbody>(true).Where(r => r.CompareTag("Ragdoll")).ToArray();
ToggleRagDoll(false);
}
public void ToggleRagDoll(bool isRagdoll)
{
this.isRagdoll = isRagdoll;
foreach (var allCollider in allColliders)
{
allCollider.enabled = isRagdoll;
}
foreach (var allRigidbody in allRigidbodies)
{
allRigidbody.isKinematic = !isRagdoll;
allRigidbody.useGravity = isRagdoll;
}
if(animator!=null) animator.enabled = !isRagdoll;
}
public void AddForce(Vector3 force)
{
if (!isRagdoll) return;
foreach (var rigidbody in allRigidbodies)
{
rigidbody.AddForce(force*Time.deltaTime);
}
}
private void OnValidate()
{
if (animator == null) animator = GetComponent<Animator>();
}
}
Drag in the Animator. When the character dies, you’ll need to call the Ragdoll script’s ToggleRagDoll() method with true as the parameter. Optionally, you can add a force at the impact location to give the ragdoll some direction.
5 Likes