Advertisement
romi_fauzi

RagdollController

Jun 7th, 2020
2,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RagdollController : MonoBehaviour
  6. {
  7.     [SerializeField] GameObject leftHandIk, rightHandIk, leftFootIk, rightFootIk;
  8.     [SerializeField] Animator animator;
  9.     public bool RagdollActive { get; private set; }
  10.  
  11.     private HingeJoint2D[] joints;
  12.     private Rigidbody2D[] rbs;
  13.  
  14.     private Dictionary<Rigidbody2D, Vector3> initialPos = new Dictionary<Rigidbody2D, Vector3>();
  15.     private Dictionary<Rigidbody2D, Quaternion> initialRot = new Dictionary<Rigidbody2D, Quaternion>();
  16.  
  17.     // Start is called before the first frame update
  18.     void Awake()
  19.     {
  20.         joints = GetComponentsInChildren<HingeJoint2D>();
  21.         rbs = GetComponentsInChildren<Rigidbody2D>();
  22.  
  23.         foreach (var rb in rbs)
  24.         {
  25.             initialPos.Add(rb, rb.transform.localPosition);
  26.             initialRot.Add(rb, rb.transform.localRotation);
  27.         }
  28.  
  29.         DisableRagdoll();
  30.     }
  31.  
  32.     void RecordTransform()
  33.     {
  34.         foreach (var rb in rbs)
  35.         {
  36.             initialPos[rb] = rb.transform.localPosition;
  37.             initialRot[rb] = rb.transform.localRotation;
  38.         }
  39.     }
  40.  
  41.     public void ActivateRagdoll()
  42.     {
  43.         RagdollActive = true;
  44.         RecordTransform(); //record last bones transform, for disabling later
  45.         animator.enabled = false;
  46.         foreach (var rb in rbs)
  47.         {
  48.             rb.bodyType = RigidbodyType2D.Dynamic;
  49.         }
  50.  
  51.         foreach (var joint in joints)
  52.         {
  53.             joint.enabled = true;
  54.         }
  55.  
  56.         leftHandIk.SetActive(false);
  57.         leftFootIk.SetActive(false);
  58.         rightHandIk.SetActive(false);
  59.         rightFootIk.SetActive(false);
  60.     }
  61.  
  62.     public void DisableRagdoll()
  63.     {
  64.         RagdollActive = false;
  65.         animator.enabled = true;
  66.         foreach (var rb in rbs)
  67.         {
  68.             rb.velocity = Vector2.zero;
  69.             rb.angularVelocity = 0f;
  70.             rb.bodyType = RigidbodyType2D.Kinematic;
  71.  
  72.             rb.transform.localPosition = initialPos[rb];
  73.             rb.transform.localRotation = initialRot[rb];
  74.         }
  75.  
  76.         foreach (var joint in joints)
  77.         {
  78.             joint.enabled = false;
  79.         }
  80.  
  81.         leftHandIk.SetActive(true);
  82.         leftFootIk.SetActive(true);
  83.         rightHandIk.SetActive(true);
  84.         rightFootIk.SetActive(true);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement