public void EnableRagdoll() { if (topRB) { topRB.isKinematic = true; } if (_currentRagdollState == false) { if (userControl) { userControl.Character.Move(Vector3.zero, false, false); userControl.enabled = false; userControl.Character.enabled = false; } if (animator) { animator.enabled = false; } // Create colliders for each body piece for (int i = 0; i < _pieces.Count; i++) { // Get this piece RagdollBodyPiece p = _pieces[i]; // Check piece is valid if (p == null) { _pieces.RemoveAt(i); i--; continue; } if (p._piece == null) { _pieces.RemoveAt(i); i--; continue; } // Generate collider Collider c = GenerateCollider(p); // Let body piece track it's collider p.collider = c; } // Attach pieces to parent pieces via joints foreach (RagdollBodyPiece p in _pieces) { AddPart(p); } _currentRagdollState = true; } }
private void AddPart(RagdollBodyPiece p) { Transform t = p._piece; RagdollBodyPiece parentPiece = null; // Find parent piece while (t.parent != null) { // Check if parent has a ragdoll piece RagdollBodyPiece piece = _pieces.Find(x => x._piece == t.parent); if (piece != null) { parentPiece = piece; break; } t = t.parent; } if (parentPiece != null) { p._parent = parentPiece; // Get parent's collider Collider parentCollider = parentPiece.collider; Rigidbody parentRB = parentPiece._piece.gameObject.GetComponent <Rigidbody>(); if (!parentRB) { parentRB = parentPiece._piece.gameObject.AddComponent <Rigidbody>(); } // Track parent's rigidbody if (!_rigidbodyComponents.Contains(parentRB)) { _rigidbodyComponents.Add(parentRB); } // Add a character joint CharacterJoint j = p._piece.gameObject.AddComponent <CharacterJoint>(); j.connectedBody = parentRB; p._joint = j; // Track rigidbody Rigidbody thisRB = p._piece.gameObject.GetComponent <Rigidbody>(); if (thisRB) { if (!_rigidbodyComponents.Contains(thisRB)) { _rigidbodyComponents.Add(thisRB); } } } }
private Transform TopRagdollPiece() { if (_pieces.Count == 0) { return(null); } RagdollBodyPiece p = _pieces[0]; while (p._parent != null) { p = p._parent; } return(p._piece); }
private Collider GenerateCollider(RagdollBodyPiece p) { if (p == null) { return(null); } if (!p._piece) { return(null); } // Create object to allow correct collider rotation & make it a child of the current piece GameObject g = new GameObject("Ragdoll Collider"); g.hideFlags = HideFlags.HideInInspector; g.transform.rotation = Quaternion.Euler(p._rotation); g.transform.position = p._piece.position; g.transform.parent = p._piece; switch (p._shape) { // TODO: Add offset case RagdollBodyPiece.COLLIDERTYPE.Sphere: { SphereCollider c = g.AddComponent <SphereCollider>(); c.radius = p._radius; c.center = p._offset; return(c); } case RagdollBodyPiece.COLLIDERTYPE.Capsule: { CapsuleCollider c = g.AddComponent <CapsuleCollider>(); c.radius = p._radius; c.height = p._height + p._radius * 2; c.center = c.transform.worldToLocalMatrix * p._offset; return(c); } default: throw new System.Exception("No shape defined for body piece."); } }