Firstborn/Assets/RPG Creation Kit/Scripts/AI/AI Scripts/Ragdoll.cs
Schaken-Mods 959e80cf72 assets upload
assets upload description.
2023-03-28 12:16:30 -05:00

94 lines
2.7 KiB
C#

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;
}
}
}