private void SaveRigidbody() { rigidbodySettings = new RigidbodySettings(); rigidbodySettings.material = rBody.sharedMaterial; rigidbodySettings.mass = rBody.mass; rigidbodySettings.drag = rBody.drag; rigidbodySettings.angularDrag = rBody.angularDrag; }
void SetRigidbodySettings(Rigidbody otherRB, RigidbodySettings settings) { otherRB.mass = settings.mass; otherRB.useGravity = settings.useGravity; otherRB.collisionDetectionMode = settings.collisionDetectionMode; otherRB.interpolation = settings.interpolation; otherRB.drag = settings.drag; otherRB.angularDrag = settings.angularDrag; }
void DropObject(ref Rigidbody otherRB, RigidbodySettings settings, bool shouldThrow) { SetRigidbodySettings(otherRB, settings); if (shouldThrow) { float accel = GetThrowAcceleration(otherRB.mass); float torque = Random.value * grabbedObjectMaxThrowTorque; otherRB.AddForce(head.transform.forward * accel, ForceMode.Acceleration); otherRB.AddTorque(Random.insideUnitSphere * torque); } otherRB = null; }
void PickupObject(Rigidbody grabbableRigidbody, out Rigidbody grabbedRB, out RigidbodySettings grabbedRBSettings) { grabbedRBSettings = new RigidbodySettings(grabbableRigidbody); // grabbableRigidbody.mass = 1f; grabbableRigidbody.useGravity = false; grabbableRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; grabbableRigidbody.interpolation = RigidbodyInterpolation.Interpolate; grabbableRigidbody.drag = 0f; grabbableRigidbody.angularDrag = grabbedObjectDefaultAngularDrag; grabbedRB = grabbableRigidbody; // grabbedRB.MovePosition(head.transform.position + (head.transform.forward * grabbedObjectMaxDistance)); }
void Start() { rb = GetComponent <Rigidbody>(); baseRbSettings = new RigidbodySettings(rb); debugGUI = new DebugGUI(); }
void OnWizardCreate() { if (!export) { string path = EditorUtility.OpenFilePanel("Load Ragdoll", "Assets", "xml"); if (path.Length != 0) { byte[] bytes = System.IO.File.ReadAllBytes(path); xml = XMLSerializer.ByteArrayToString(bytes); RagdollLoader.Load(xml, rootBone, true); } } else { if ((Selection.gameObjects == null) || (Selection.gameObjects.Length != 1)) { EditorUtility.DisplayDialog("Wrong selection", "Please select root bone to export the ragdoll", "OK"); return; } path = EditorUtility.SaveFilePanel("Save Ragdoll", "Assets", "Ragdoll", "xml"); GameObject gameObject = rootBone; List <RagdollJoint> ragdollJoints = new List <RagdollJoint>(); CharacterJoint[] jointsFromChildren = gameObject.GetComponents <CharacterJoint>(); if (jointsFromChildren.Length == 0) { Collider rootCollider = gameObject.GetComponent <Collider>(); Rigidbody rootRigidBody = gameObject.GetComponent <Rigidbody>(); if (rootCollider == null || rootRigidBody == null) { Debug.LogError("Ragdoll Exporter: root bone needs a collider and a rigidbody!"); return; } BoxColliderSettings boxColliderSettings = null; SphereColliderSettings sphereColliderSettings = null; CapsuleColliderSettings capsuleColliderSettings = null; if (rootCollider is BoxCollider) { BoxCollider cast = (BoxCollider)rootCollider; boxColliderSettings = new BoxColliderSettings(); boxColliderSettings.center = cast.center.ToString("G4"); boxColliderSettings.size = cast.size.ToString("G4"); } else if (rootCollider is SphereCollider) { SphereCollider cast = (SphereCollider)rootCollider; sphereColliderSettings = new SphereColliderSettings(); sphereColliderSettings.center = cast.center.ToString("G4"); sphereColliderSettings.radius = cast.radius; } else if (rootCollider is CapsuleCollider) { CapsuleCollider cast = (CapsuleCollider)rootCollider; capsuleColliderSettings = new CapsuleColliderSettings(); capsuleColliderSettings.center = cast.center.ToString("G4"); capsuleColliderSettings.radius = cast.radius; capsuleColliderSettings.height = cast.height; capsuleColliderSettings.direction = cast.direction; } RigidbodySettings rigidbodySettings = new RigidbodySettings(); rigidbodySettings.mass = rootRigidBody.mass; RagdollJoint ragdollJoint = new RagdollJoint(); ragdollJoint.boneName = gameObject.name; ragdollJoint.characterJointSettings = null; ragdollJoint.boxColliderSettings = boxColliderSettings; ragdollJoint.sphereColliderSettings = sphereColliderSettings; ragdollJoint.capsuleColliderSettings = capsuleColliderSettings; ragdollJoint.rigidbodySettings = rigidbodySettings; ragdollJoints.Add(ragdollJoint); } else { EditorUtility.DisplayDialog("Wrong selection", "Please select root bone to export the ragdoll", "OK"); return; } // Iterate over all character joints because it is easy to get rigidbody and collider from a joint but not the other way around jointsFromChildren = gameObject.GetComponentsInChildren <CharacterJoint>(); Debug.Log("Ragdoll Exporter: " + gameObject.name + " joint processed"); foreach (CharacterJoint joint in jointsFromChildren) { Rigidbody rigidbody = joint.gameObject.GetComponent <Rigidbody>(); Collider collider = joint.gameObject.GetComponent <Collider>(); if (rigidbody == null || collider == null) { Debug.LogWarning("Ragdoll Exporter: bone with CharacterJoint is missing a collider or rigidbody (" + joint.name + ")"); continue; } CharacterJointSettings characterJointSettings = new CharacterJointSettings(); characterJointSettings.connectedBody = joint.connectedBody.name; characterJointSettings.anchor = joint.anchor.ToString("G4"); characterJointSettings.axis = joint.axis.ToString("G4"); characterJointSettings.connectedAnchor = joint.connectedAnchor.ToString("G4"); characterJointSettings.swingAxis = joint.swingAxis.ToString("G4"); characterJointSettings.lowTwistLimit_Bounciness = joint.lowTwistLimit.bounciness; characterJointSettings.lowTwistLimit_Limit = joint.lowTwistLimit.limit; characterJointSettings.highTwistLimit_Bounciness = joint.highTwistLimit.bounciness; characterJointSettings.highTwistLimit_Limit = joint.highTwistLimit.limit; characterJointSettings.swing1Limit_Bounciness = joint.swing1Limit.bounciness; characterJointSettings.swing1Limit_Limit = joint.swing1Limit.limit; characterJointSettings.swing2Limit_Bounciness = joint.swing2Limit.bounciness; characterJointSettings.swing2Limit_Limit = joint.swing2Limit.limit; BoxColliderSettings boxColliderSettings = null; SphereColliderSettings sphereColliderSettings = null; CapsuleColliderSettings capsuleColliderSettings = null; if (collider is BoxCollider) { BoxCollider cast = (BoxCollider)collider; boxColliderSettings = new BoxColliderSettings(); boxColliderSettings.center = cast.center.ToString("G4"); boxColliderSettings.size = cast.size.ToString("G4"); } else if (collider is SphereCollider) { SphereCollider cast = (SphereCollider)collider; sphereColliderSettings = new SphereColliderSettings(); sphereColliderSettings.center = cast.center.ToString("G4"); sphereColliderSettings.radius = cast.radius; } else if (collider is CapsuleCollider) { CapsuleCollider cast = (CapsuleCollider)collider; capsuleColliderSettings = new CapsuleColliderSettings(); capsuleColliderSettings.center = cast.center.ToString("G4"); capsuleColliderSettings.radius = cast.radius; capsuleColliderSettings.height = cast.height; capsuleColliderSettings.direction = cast.direction; } RigidbodySettings rigidbodySettings = new RigidbodySettings(); rigidbodySettings.mass = rigidbody.mass; RagdollJoint ragdollJoint = new RagdollJoint(); ragdollJoint.boneName = joint.name; ragdollJoint.characterJointSettings = characterJointSettings; ragdollJoint.boxColliderSettings = boxColliderSettings; ragdollJoint.sphereColliderSettings = sphereColliderSettings; ragdollJoint.capsuleColliderSettings = capsuleColliderSettings; ragdollJoint.rigidbodySettings = rigidbodySettings; ragdollJoints.Add(ragdollJoint); Debug.Log("Ragdoll Exporter: " + joint.name + " joint processed"); } if (path == null || path == "") { Debug.Log("Ragdoll Exporter: operation cancelled"); return; } Ragdoll rD = new Ragdoll(); rD.ragdollJoints = ragdollJoints.ToArray(); string xml = XMLSerializer.SerializeObject(rD); using (StreamWriter writer = new StreamWriter(path, false)) { try { writer.Write(xml); } catch (System.Exception ex) { string msg = " threw:\n" + ex.ToString(); Debug.LogError(msg); EditorUtility.DisplayDialog("Error on export", msg, "OK"); } } Debug.Log("Ragdoll Exporter: " + (jointsFromChildren.Length + 1).ToString() + " joints processed"); Debug.Log("Success!"); } }