using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitRagdoll : MonoBehaviour
{
[SerializeField] private Transform ragdollRootBone;
public void Setup(Transform originalRootBone) {
MatchAllChildTransforms(originalRootBone, ragdollRootBone);
float explosionForce = 300f;
float explosionRange = 10f;
Vector3 randorDirection = new Vector3(Random.Range(-1f, +1f), 0, Random.Range(-1f, +1f));
ApplyExplosionToRagdoll(ragdollRootBone, explosionForce, transform.position + randorDirection, explosionRange);
}
private void MatchAllChildTransforms(Transform root, Transform clone) {
foreach (Transform child in root) {
Transform cloneChild = clone.Find(child.name);
if (cloneChild != null) {
cloneChild.position = child.position;
cloneChild.rotation = child.rotation;
MatchAllChildTransforms(child, cloneChild);
}
}
}
private void ApplyExplosionToRagdoll(Transform root, float explosionForce, Vector3 explosionPosition, float explosionRange) {
foreach (Transform child in root) {
if (child.TryGetComponent<Rigidbody>(out Rigidbody childRigidBody)) {
childRigidBody.AddExplosionForce(explosionForce, explosionPosition,explosionRange);
}
ApplyExplosionToRagdoll(child, explosionForce, explosionPosition, explosionRange);
}
}
}