using System.Collections; using System.Collections.Generic; using UnityEngine; using RPGCreationKit; using RPGCreationKit.SaveSystem; namespace RPGCreationKit { public class Ragdoll : MonoBehaviour { public Animator animator; [SerializeField] Rigidbody[] rigidbodies; [SerializeField] Collider[] colliders; bool isInRagdoll = false; private void OnEnable() { if (animator == null) animator = GetComponentInParent(); } // Start is called before the first frame update void Start() { if (animator == null) animator = GetComponentInParent(); foreach (Rigidbody ragdollBone in rigidbodies) { ragdollBone.collisionDetectionMode = CollisionDetectionMode.Discrete; ragdollBone.isKinematic = true; } } [ContextMenu("Force Ragdoll")] public void ForceRagdoll() { animator.enabled = false; foreach (Rigidbody ragdollBone in rigidbodies) { ragdollBone.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative; ragdollBone.isKinematic = false; } } public void ToggleRagdoll(bool bisAnimating) { isInRagdoll = !bisAnimating; foreach (Rigidbody ragdollBone in rigidbodies) { if (bisAnimating) { ragdollBone.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative; } else { ragdollBone.collisionDetectionMode = CollisionDetectionMode.Discrete; } ragdollBone.isKinematic = bisAnimating; } animator.enabled = bisAnimating; } [ContextMenu("Find Rigidbodies")] private void FindRigidbodies() { rigidbodies = GetComponentsInChildren(); } public void LoadFromSaveData(RagdollSaveData data) { for (int i = 0; i < data.ragdollElements.Count; i++) { rigidbodies[i].transform.position = data.ragdollElements[i].pos; rigidbodies[i].transform.rotation = data.ragdollElements[i].rot; } } public RagdollSaveData ToSaveData() { RagdollSaveData data = new RagdollSaveData(); for (int i = 0; i < rigidbodies.Length; i++) data.ragdollElements.Add(new RagdollSaveData.RagdollElement(rigidbodies[i].position, rigidbodies[i].rotation)); return data; } } }