private void OnEnable() { tgt = (RamecanMixer)target; if (tgt.bones == null) { return; } rects = new Rect[tgt.bones.Count]; selected = new bool[tgt.bones.Count]; if (tgt.states.Count == 0) { return; } stateName = tgt.states[tgt.currentState].name; stateTransitionSmooth = tgt.states[tgt.currentState].transitionSmooth; }
//creating objects and adding components public void CreateRagdoll() { if (transform.parent != null && transform.parent.name.Equals(transform.name + " Container")) { Transform parent = transform.parent; transform.parent = null; DestroyImmediate(parent.gameObject); } Transform container = new GameObject().transform; Transform ragdoll = new GameObject().transform; container.position = ragdoll.position = transform.position; container.rotation = ragdoll.rotation = transform.rotation; container.name = transform.name + " Container"; ragdoll.name = "Ragdoll"; transform.parent = ragdoll.parent = container; List <Rigidbody> rigidbodies = new List <Rigidbody>(); foreach (PreviewBone bone in previewBones) { GameObject boneGo = new GameObject(); Transform boneTr = boneGo.transform; boneTr.parent = ragdoll; boneTr.name = bone.name; boneTr.position = bone.tr.position; boneTr.rotation = transform.rotation * bone.rotation; boneGo.layer = layer; if (bone.colliderType == ColliderType.Box) { BoxCollider collider = boneGo.AddComponent <BoxCollider>(); collider.center = bone.bounds.center; collider.size = bone.bounds.size; collider.material = physicMaterial; } else if (bone.colliderType == ColliderType.Capsule) { CapsuleCollider collider = boneGo.AddComponent <CapsuleCollider>(); collider.center = bone.bounds.center; collider.radius = bone.capsuleBounds.radius; collider.height = bone.capsuleBounds.height; collider.direction = (int)bone.capsuleBounds.direction; collider.material = physicMaterial; } else if (bone.colliderType == ColliderType.Sphere) { SphereCollider collider = boneGo.AddComponent <SphereCollider>(); collider.center = bone.bounds.center; collider.radius = bone.capsuleBounds.radius; collider.material = physicMaterial; } Rigidbody rigidbody = boneGo.AddComponent <Rigidbody>(); rigidbody.mass = Mathf.Round(mass * bone.massInPercent * 100) / 100; rigidbodies.Add(rigidbody); if (bone.IsRoot) { continue; } ConfigurableJoint joint = boneGo.AddComponent <ConfigurableJoint>(); joint.connectedBody = rigidbodies[bone.parentID]; joint.autoConfigureConnectedAnchor = false; joint.rotationDriveMode = RotationDriveMode.Slerp; joint.xMotion = joint.yMotion = joint.zMotion = ConfigurableJointMotion.Locked; joint.angularXMotion = joint.angularYMotion = joint.angularZMotion = ConfigurableJointMotion.Limited; joint.projectionMode = JointProjectionMode.PositionAndRotation; joint.enablePreprocessing = true; joint.axis = bone.axis; joint.secondaryAxis = bone.swingAxis; joint.lowAngularXLimit = new SoftJointLimit { limit = bone.lowTwistLimit }; joint.highAngularXLimit = new SoftJointLimit { limit = bone.highTwistLimit }; joint.angularYLimit = new SoftJointLimit { limit = bone.swing1Limit }; joint.angularZLimit = new SoftJointLimit { limit = bone.swing2Limit }; } RamecanMixer mixer = gameObject.GetComponent <RamecanMixer>(); if (mixer != null) { mixer.Reset(); } else { mixer = gameObject.AddComponent <RamecanMixer>(); } mixer.SetDrive(spring, damper); Animator animator = gameObject.GetComponent <Animator>(); if (animator != null) { animator.cullingMode = AnimatorCullingMode.AlwaysAnimate; } }