Firstborn/Assets/RPG Creation Kit/Scripts/AI/AI Scripts/Ragdoll.cs

94 lines
2.7 KiB
C#
Raw Normal View History

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<Animator>();
}
// Start is called before the first frame update
void Start()
{
if (animator == null)
animator = GetComponentInParent<Animator>();
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<Rigidbody>();
}
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;
}
}
}