add Physics support

This commit is contained in:
1605296300@qq.com
2022-08-10 16:54:20 +08:00
parent 33c8d35c84
commit f797fbb2fd
855 changed files with 649609 additions and 98 deletions
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: ea7ca53e3614b4fd780877c7354eb1e0
folderAsset: yes
timeCreated: 1444739072
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

@@ -0,0 +1,57 @@
fileFormatVersion: 2
guid: abe07bc040eff524eb2e03d5fbbbd902
timeCreated: 1444736887
licenseType: Store
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 0
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: 4
maxTextureSize: 128
textureSettings:
filterMode: -1
aniso: 16
mipBias: -1
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 1
textureType: 5
buildTargetSettings: []
spriteSheet:
sprites: []
outline: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

@@ -0,0 +1,57 @@
fileFormatVersion: 2
guid: 07d937e4fdaf46a498a8bfc405e39ccf
timeCreated: 1444736140
licenseType: Store
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 0
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 8
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: 4
maxTextureSize: 128
textureSettings:
filterMode: -1
aniso: 16
mipBias: -1
wrapMode: 1
nPOTScale: 1
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 1
textureType: 5
buildTargetSettings: []
spriteSheet:
sprites: []
outline: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 29bd1d5057d5e4d0eb6fe88dd997bad6
folderAsset: yes
timeCreated: 1429874947
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,490 @@
using UnityEngine;
using System.Collections;
namespace RootMotion.Dynamics {
/// <summary>
/// Automatically generates a ragdoll for a Biped character.
/// </summary>
[HelpURL("https://www.youtube.com/watch?v=y-luLRVmL7E&index=1&list=PLVxSIA1OaTOuE2SB9NUbckQ9r2hTg4mvL")]
[AddComponentMenu("Scripts/RootMotion.Dynamics/Ragdoll Manager/Biped Ragdoll Creator")]
public class BipedRagdollCreator : RagdollCreator {
// Open the User Manual URL
[ContextMenu("User Manual")]
void OpenUserManual() {
Application.OpenURL("http://root-motion.com/puppetmasterdox/html/page1.html");
}
// Open the Script Reference URL
[ContextMenu("Scrpt Reference")]
void OpenScriptReference() {
Application.OpenURL("http://root-motion.com/puppetmasterdox/html/class_root_motion_1_1_dynamics_1_1_biped_ragdoll_creator.html#details");
}
// Open a video tutorial about setting up the component
[ContextMenu("TUTORIAL VIDEO")]
void OpenTutorial() {
Application.OpenURL("https://www.youtube.com/watch?v=y-luLRVmL7E&index=1&list=PLVxSIA1OaTOuE2SB9NUbckQ9r2hTg4mvL");
}
public bool canBuild;
public BipedRagdollReferences references;
public Options options = Options.Default;
[System.Serializable]
public struct Options {
//[SpaceAttribute(10)]
public float weight;
[HeaderAttribute("Optional Bones")]
public bool spine;
public bool chest;
public bool hands;
public bool feet;
[HeaderAttribute("Joints")]
public JointType joints;
public float jointRange;
[HeaderAttribute("Colliders")]
public float colliderLengthOverlap;
public ColliderType torsoColliders;
public ColliderType headCollider;
public ColliderType armColliders;
public ColliderType handColliders;
public ColliderType legColliders;
public ColliderType footColliders;
public static Options Default {
get {
Options o = new Options();
// General Settings
o.weight = 75f;
o.colliderLengthOverlap = 0.1f;
o.jointRange = 1f;
o.chest = true;
o.headCollider = ColliderType.Capsule;
o.armColliders = ColliderType.Capsule;
o.hands = true;
o.handColliders = ColliderType.Capsule;
o.legColliders = ColliderType.Capsule;
o.feet = true;
return o;
}
}
}
public static Options AutodetectOptions(BipedRagdollReferences r) {
Options o = Options.Default;
if (r.spine == null) o.spine = false;
if (r.chest == null) o.chest = false;
// If chest bone is too high
if (o.chest && Vector3.Dot(r.root.up, r.chest.position - GetUpperArmCentroid(r)) > 0f) {
o.chest = false;
if (r.spine != null) o.spine = true;
}
return o;
}
public static void Create(BipedRagdollReferences r, Options options) {
string msg = string.Empty;
if (!r.IsValid(ref msg)) {
Debug.LogWarning(msg);
return;
}
// Clean up
ClearAll(r.root);
// Colliders
CreateColliders(r, options);
// Rigidbodies
MassDistribution(r, options);
// Joints
CreateJoints(r, options);
}
private static void CreateColliders(BipedRagdollReferences r, Options options) {
// Torso
Vector3 upperArmToHeadCentroid = GetUpperArmToHeadCentroid(r);
if (r.spine == null) options.spine = false;
if (r.chest == null) options.chest = false;
Vector3 shoulderDirection = r.rightUpperArm.position - r.leftUpperArm.position;
float torsoWidth = shoulderDirection.magnitude;
float torsoProportionAspect = 0.6f;
// Hips
Vector3 hipsStartPoint = r.hips.position;
// Making sure the hip bone is not at the feet
float toHead = Vector3.Distance(r.head.position, r.root.position);
float toHips = Vector3.Distance(r.hips.position, r.root.position);
if (toHips < toHead * 0.2f) {
hipsStartPoint = Vector3.Lerp(r.leftUpperLeg.position, r.rightUpperLeg.position, 0.5f);
}
Vector3 lastEndPoint = options.spine? r.spine.position: (options.chest? r.chest.position: upperArmToHeadCentroid);
hipsStartPoint += (hipsStartPoint - upperArmToHeadCentroid) * 0.1f;
float hipsWidth = options.spine || options.chest? torsoWidth * 0.8f: torsoWidth;
CreateCollider(r.hips, hipsStartPoint, lastEndPoint, options.torsoColliders, options.colliderLengthOverlap, hipsWidth, torsoProportionAspect, shoulderDirection);
// Spine
if (options.spine) {
Vector3 spineStartPoint = lastEndPoint;
lastEndPoint = options.chest? r.chest.position: upperArmToHeadCentroid;
float spineWidth = options.chest? torsoWidth * 0.75f: torsoWidth;
CreateCollider(r.spine, spineStartPoint, lastEndPoint, options.torsoColliders, options.colliderLengthOverlap, spineWidth, torsoProportionAspect, shoulderDirection);
}
if (options.chest) {
Vector3 chestStartPoint = lastEndPoint;
lastEndPoint = upperArmToHeadCentroid;
CreateCollider(r.chest, chestStartPoint, lastEndPoint, options.torsoColliders, options.colliderLengthOverlap, torsoWidth, torsoProportionAspect, shoulderDirection);
}
// Head
Vector3 headStartPoint = lastEndPoint;
Vector3 headEndPoint = headStartPoint + (headStartPoint - hipsStartPoint) * 0.45f;
Vector3 axis = r.head.TransformVector(AxisTools.GetAxisVectorToDirection(r.head, headEndPoint - headStartPoint));
headEndPoint = headStartPoint + Vector3.Project(headEndPoint - headStartPoint, axis).normalized * (headEndPoint - headStartPoint).magnitude;
CreateCollider(r.head, headStartPoint, headEndPoint, options.headCollider, options.colliderLengthOverlap, Vector3.Distance(headStartPoint, headEndPoint) * 0.8f);
// Arms
float armWidthAspect = 0.4f;
float leftArmWidth = Vector3.Distance(r.leftUpperArm.position, r.leftLowerArm.position) * armWidthAspect;
CreateCollider(r.leftUpperArm, r.leftUpperArm.position, r.leftLowerArm.position, options.armColliders, options.colliderLengthOverlap, leftArmWidth);
CreateCollider(r.leftLowerArm, r.leftLowerArm.position, r.leftHand.position, options.armColliders, options.colliderLengthOverlap, leftArmWidth * 0.9f);
float rightArmWidth = Vector3.Distance(r.rightUpperArm.position, r.rightLowerArm.position) * armWidthAspect;
CreateCollider(r.rightUpperArm, r.rightUpperArm.position, r.rightLowerArm.position, options.armColliders, options.colliderLengthOverlap, rightArmWidth);
CreateCollider(r.rightLowerArm, r.rightLowerArm.position, r.rightHand.position, options.armColliders, options.colliderLengthOverlap, rightArmWidth * 0.9f);
// Legs
float legWidthAspect = 0.3f;
float leftLegWidth = Vector3.Distance(r.leftUpperLeg.position, r.leftLowerLeg.position) * legWidthAspect;
CreateCollider(r.leftUpperLeg, r.leftUpperLeg.position, r.leftLowerLeg.position, options.legColliders, options.colliderLengthOverlap, leftLegWidth);
CreateCollider(r.leftLowerLeg, r.leftLowerLeg.position, r.leftFoot.position, options.legColliders, options.colliderLengthOverlap, leftLegWidth * 0.9f);
float rightLegWidth = Vector3.Distance(r.rightUpperLeg.position, r.rightLowerLeg.position) * legWidthAspect;
CreateCollider(r.rightUpperLeg, r.rightUpperLeg.position, r.rightLowerLeg.position, options.legColliders, options.colliderLengthOverlap, rightLegWidth);
CreateCollider(r.rightLowerLeg, r.rightLowerLeg.position, r.rightFoot.position, options.legColliders, options.colliderLengthOverlap, rightLegWidth * 0.9f);
// Hands
if (options.hands) {
CreateHandCollider(r.leftHand, r.leftLowerArm, r.root, options);
CreateHandCollider(r.rightHand, r.rightLowerArm, r.root, options);
}
// Feet
if (options.feet) {
CreateFootCollider(r.leftFoot, r.leftLowerLeg, r.leftUpperLeg, r.root, options);
CreateFootCollider(r.rightFoot, r.rightLowerLeg, r.rightUpperLeg, r.root, options);
}
}
private static void CreateHandCollider(Transform hand, Transform lowerArm, Transform root, Options options) {
Vector3 axis = hand.TransformVector(AxisTools.GetAxisVectorToPoint(hand, GetChildCentroid(hand, lowerArm.position)));
Vector3 endPoint = hand.position - (lowerArm.position - hand.position) * 0.75f;
endPoint = hand.position + Vector3.Project(endPoint - hand.position, axis).normalized * (endPoint - hand.position).magnitude;
CreateCollider(hand, hand.position, endPoint, options.handColliders, options.colliderLengthOverlap, Vector3.Distance(endPoint, hand.position) * 0.5f);
}
private static void CreateFootCollider(Transform foot, Transform lowerLeg, Transform upperLeg, Transform root, Options options) {
float legHeight = (upperLeg.position - foot.position).magnitude;
Vector3 axis = foot.TransformVector(AxisTools.GetAxisVectorToPoint(foot, GetChildCentroid(foot, foot.position + root.forward) + root.forward * legHeight * 0.2f));
Vector3 endPoint = foot.position + root.forward * legHeight * 0.25f;
endPoint = foot.position + Vector3.Project(endPoint - foot.position, axis).normalized * (endPoint - foot.position).magnitude;
float width = Vector3.Distance(endPoint, foot.position) * 0.5f;
Vector3 startPoint = foot.position;
bool footBelowRoot = Vector3.Dot(root.up, foot.position - root.position) < 0f;
Vector3 heightOffset = footBelowRoot? Vector3.zero: Vector3.Project((startPoint - root.up * width * 0.5f) - root.position, root.up);
Vector3 direction = endPoint - startPoint;
startPoint -= direction * 0.2f;
CreateCollider(foot, startPoint - heightOffset, endPoint - heightOffset, options.footColliders, options.colliderLengthOverlap, width);
}
private static Vector3 GetChildCentroid(Transform t, Vector3 fallback) {
if (t.childCount == 0) return fallback;
Vector3 c = Vector3.zero;
for (int i = 0; i < t.childCount; i++) {
c += t.GetChild(i).position;
}
c /= (float)t.childCount;
return c;
}
private static void MassDistribution(BipedRagdollReferences r, Options o) {
int torsoBones = 3;
if (r.spine == null) {
o.spine = false;
torsoBones --;
}
if (r.chest == null) {
o.chest = false;
torsoBones --;
}
float torsoPrc = 0.508f / (float)torsoBones;
float headPrc = 0.0732f;
float upperArmPrc = 0.027f;
float lowerArmPrc = 0.016f;
float handPrc = 0.0066f;
float upperLegPrc = 0.0988f;
float lowerLegPrc = 0.0465f;
float footPrc = 0.0145f;
r.hips.GetComponent<Rigidbody>().mass = torsoPrc * o.weight;
if (o.spine) r.spine.GetComponent<Rigidbody>().mass = torsoPrc * o.weight;
if (o.chest) r.chest.GetComponent<Rigidbody>().mass = torsoPrc * o.weight;
r.head.GetComponent<Rigidbody>().mass = headPrc * o.weight;
r.leftUpperArm.GetComponent<Rigidbody>().mass = upperArmPrc * o.weight;
r.rightUpperArm.GetComponent<Rigidbody>().mass = r.leftUpperArm.GetComponent<Rigidbody>().mass;
r.leftLowerArm.GetComponent<Rigidbody>().mass = lowerArmPrc * o.weight;
r.rightLowerArm.GetComponent<Rigidbody>().mass = r.leftLowerArm.GetComponent<Rigidbody>().mass;
if (o.hands) {
r.leftHand.GetComponent<Rigidbody>().mass = handPrc * o.weight;
r.rightHand.GetComponent<Rigidbody>().mass = r.leftHand.GetComponent<Rigidbody>().mass;
}
r.leftUpperLeg.GetComponent<Rigidbody>().mass = upperLegPrc * o.weight;
r.rightUpperLeg.GetComponent<Rigidbody>().mass = r.leftUpperLeg.GetComponent<Rigidbody>().mass;
r.leftLowerLeg.GetComponent<Rigidbody>().mass = lowerLegPrc * o.weight;
r.rightLowerLeg.GetComponent<Rigidbody>().mass = r.leftLowerLeg.GetComponent<Rigidbody>().mass;
if (o.feet) {
r.leftFoot.GetComponent<Rigidbody>().mass = footPrc * o.weight;
r.rightFoot.GetComponent<Rigidbody>().mass = r.leftFoot.GetComponent<Rigidbody>().mass;
}
}
private static void CreateJoints(BipedRagdollReferences r, Options o) {
// Torso
if (r.spine == null) o.spine = false;
if (r.chest == null) o.chest = false;
float spineMinSwing = -30f * o.jointRange;
float spineMaxSwing = 10f * o.jointRange;
float spineSwing2 = 25f * o.jointRange;
float spineTwist = 25f * o.jointRange;
CreateJoint(new CreateJointParams(
r.hips.GetComponent<Rigidbody>(),
null,
(o.spine? r.spine: (o.chest? r.chest: r.head)),
r.root.right,
new CreateJointParams.Limits(0f, 0f, 0f, 0f),
o.joints
));
if (o.spine) {
CreateJoint(new CreateJointParams(
r.spine.GetComponent<Rigidbody>(),
r.hips.GetComponent<Rigidbody>(),
(o.chest? r.chest: r.head),
r.root.right,
new CreateJointParams.Limits(spineMinSwing, spineMaxSwing, spineSwing2, spineTwist),
o.joints
));
}
if (o.chest) {
CreateJoint(new CreateJointParams(
r.chest.GetComponent<Rigidbody>(),
(o.spine? r.spine.GetComponent<Rigidbody>(): r.hips.GetComponent<Rigidbody>()),
r.head,
r.root.right,
new CreateJointParams.Limits(spineMinSwing, spineMaxSwing, spineSwing2, spineTwist),
o.joints
));
}
// Head
Transform lastTorsoBone = o.chest? r.chest: (o.spine? r.spine: r.hips);
CreateJoint(new CreateJointParams(
r.head.GetComponent<Rigidbody>(),
lastTorsoBone.GetComponent<Rigidbody>(),
null,
r.root.right,
new CreateJointParams.Limits(-30f, 30f, 30f, 85f),
o.joints
));
// Arms
CreateJointParams.Limits upperArmLimits = new CreateJointParams.Limits(-50f * o.jointRange, 30f * o.jointRange, 177f * o.jointRange, 30 * o.jointRange);
CreateJointParams.Limits lowerArmLimits = new CreateJointParams.Limits(0f, 100f * o.jointRange, 10f * o.jointRange, 45f * o.jointRange);
CreateJointParams.Limits handLimits = new CreateJointParams.Limits(-30f * o.jointRange, 30f * o.jointRange, 30f * o.jointRange, 25f * o.jointRange);
// Left Arm
CreateLimbJoints(
lastTorsoBone,
r.leftUpperArm,
r.leftLowerArm,
r.leftHand,
r.root,
-r.root.right,
o.joints,
upperArmLimits,
lowerArmLimits,
handLimits);
// Right Arm
CreateLimbJoints(
lastTorsoBone,
r.rightUpperArm,
r.rightLowerArm,
r.rightHand,
r.root,
r.root.right,
o.joints,
upperArmLimits,
lowerArmLimits,
handLimits);
// Legs
CreateJointParams.Limits upperLegLimits = new CreateJointParams.Limits(0f * o.jointRange, 177f * o.jointRange, 25f * o.jointRange, 25 * o.jointRange);
CreateJointParams.Limits lowerLegLimits = new CreateJointParams.Limits(0f, 100f * o.jointRange, 10f * o.jointRange, 35f * o.jointRange);
CreateJointParams.Limits footLimits = new CreateJointParams.Limits(-30f * o.jointRange, 30f * o.jointRange, 30f * o.jointRange, 25f * o.jointRange);
// Left Leg
CreateLimbJoints(
r.hips,
r.leftUpperLeg,
r.leftLowerLeg,
r.leftFoot,
r.root,
-r.root.up,
o.joints,
upperLegLimits,
lowerLegLimits,
footLimits);
// Right Leg
CreateLimbJoints(
r.hips,
r.rightUpperLeg,
r.rightLowerLeg,
r.rightFoot,
r.root,
-r.root.up,
o.joints,
upperLegLimits,
lowerLegLimits,
footLimits);
}
private static void CreateLimbJoints(Transform connectedBone, Transform bone1, Transform bone2, Transform bone3, Transform root, Vector3 defaultWorldDirection, JointType jointType, CreateJointParams.Limits limits1, CreateJointParams.Limits limits2, CreateJointParams.Limits limits3) {
Quaternion bone1DefaultLocalRotation = bone1.localRotation;
bone1.rotation = Quaternion.FromToRotation(bone1.rotation * (bone2.position - bone1.position), defaultWorldDirection) * bone1.rotation;
Vector3 bone1Dir = (bone2.position - bone1.position).normalized;
Vector3 bone2Dir = (bone3.position - bone2.position).normalized;
Vector3 bendPlaneNormal = -Vector3.Cross(bone1Dir, bone2Dir);
float bone2PoseAngleOffset = Vector3.Angle(bone1Dir, bone2Dir);
bool isVertical = Mathf.Abs(Vector3.Dot(bone1Dir, root.up)) > 0.5f;
float verticalAngleOffsetMlp = isVertical? 100f: 1f; // Fixing Mixamo's inverted legs
// Fixing straight limbs
if (bone2PoseAngleOffset < 0.01f * verticalAngleOffsetMlp) {
if (isVertical) bendPlaneNormal = Vector3.Dot(bone1Dir, root.up) > 0f? root.right: -root.right;
else bendPlaneNormal = Vector3.Dot(bone1Dir, root.right) > 0f? root.up: -root.up;
//Debug.LogWarning("Limb " + bone1.name + ", " + bone2.name + ", " + bone3.name + " appears to be completely stretched out, Ragdoll Creator can not know how to assign joint limits. Please rotate the elbow/knee bone slightly towards it's natural bending direction.");
}
CreateJoint(new CreateJointParams(
bone1.GetComponent<Rigidbody>(),
connectedBone.GetComponent<Rigidbody>(),
bone2,
bendPlaneNormal,
limits1,
jointType
));
CreateJoint(new CreateJointParams(
bone2.GetComponent<Rigidbody>(),
bone1.GetComponent<Rigidbody>(),
bone3,
bendPlaneNormal,
new CreateJointParams.Limits(limits2.minSwing - bone2PoseAngleOffset, limits2.maxSwing - bone2PoseAngleOffset, limits2.swing2, limits2.twist),
jointType
));
if (bone3.GetComponent<Rigidbody>() != null) {
CreateJoint(new CreateJointParams(
bone3.GetComponent<Rigidbody>(),
bone2.GetComponent<Rigidbody>(),
null,
bendPlaneNormal,
limits3,
jointType
));
}
bone1.localRotation = bone1DefaultLocalRotation;
}
public static void ClearBipedRagdoll(BipedRagdollReferences r) {
var transforms = r.GetRagdollTransforms();
foreach (Transform t in transforms) ClearTransform(t);
}
public static bool IsClear(BipedRagdollReferences r) {
var transforms = r.GetRagdollTransforms();
foreach (Transform t in transforms) {
if (t.GetComponent<Rigidbody>() != null) return false;
}
return true;
}
private static Vector3 GetUpperArmToHeadCentroid(BipedRagdollReferences r) {
return Vector3.Lerp(GetUpperArmCentroid(r), r.head.position, 0.5f);
}
private static Vector3 GetUpperArmCentroid(BipedRagdollReferences r) {
return Vector3.Lerp(r.leftUpperArm.position, r.rightUpperArm.position, 0.5f);
}
}
}
@@ -0,0 +1,17 @@
fileFormatVersion: 2
guid: 66935e5c9035c46448e9559f0016bc79
labels:
- Ragdoll
- Wizard
- Joints
- Creator
timeCreated: 1444739181
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: abe07bc040eff524eb2e03d5fbbbd902, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,297 @@
using UnityEngine;
using System.Collections;
using RootMotion;
namespace RootMotion.Dynamics {
/// <summary>
/// Holds references to all Transforms required for a biped ragdoll.
/// </summary>
[System.Serializable]
public struct BipedRagdollReferences {
/// <summary>
/// The root transform is the parent of all the biped's bones and should be located at ground level.
/// </summary>
public Transform root;
/// <summary>
/// The pelvis bone.
/// </summary>
public Transform hips;
/// <summary>
/// The middle spine bone.
/// </summary>
public Transform spine;
/// <summary>
/// The last spine bone.
/// </summary>
public Transform chest;
/// <summary>
/// The head.
/// </summary>
public Transform head;
/// <summary>
/// The first bone of the left leg.
/// </summary>
public Transform leftUpperLeg;
/// <summary>
/// The second bone of the left leg.
/// </summary>
public Transform leftLowerLeg;
/// <summary>
/// The third bone of the left leg.
/// </summary>
public Transform leftFoot;
/// <summary>
/// The first bone of the right leg.
/// </summary>
public Transform rightUpperLeg;
/// <summary>
/// The second bone of the right leg.
/// </summary>
public Transform rightLowerLeg;
/// <summary>
/// The third bone of the right leg.
/// </summary>
public Transform rightFoot;
/// <summary>
/// The first bone of the left arm.
/// </summary>
public Transform leftUpperArm;
/// <summary>
/// The second bone of the left arm.
/// </summary>
public Transform leftLowerArm;
/// <summary>
/// The third bone of the left arm.
/// </summary>
public Transform leftHand;
/// <summary>
/// The first bone of the right arm.
/// </summary>
public Transform rightUpperArm;
/// <summary>
/// The second bone of the right arm.
/// </summary>
public Transform rightLowerArm;
/// <summary>
/// The third bone of the right arm.
/// </summary>
public Transform rightHand;
public bool IsValid(ref string msg) {
if (root == null ||
hips == null ||
head == null ||
leftUpperArm == null ||
leftLowerArm == null ||
leftHand == null ||
rightUpperArm == null ||
rightLowerArm == null ||
rightHand == null ||
leftUpperLeg == null ||
leftLowerLeg == null ||
leftFoot == null ||
rightUpperLeg == null ||
rightLowerLeg == null ||
rightFoot == null
)
{
msg = "Invalid References, one or more Transforms missing.";
return false;
}
var array = new Transform[15] {
root, hips, head, leftUpperArm, leftLowerArm, leftHand, rightUpperArm, rightLowerArm, rightHand, leftUpperLeg, leftLowerLeg, leftFoot, rightUpperLeg, rightLowerLeg, rightFoot
};
// Check if all are parented to the root
for (int i = 1; i < array.Length; i++) {
if (!IsChildRecursive(array[i], root)) {
msg = "Invalid References, " + array[i].name + " is not in the Root's hierarchy.";
return false;
}
}
// Check for duplicates
for (int i = 0; i < array.Length; i++) {
for (int i2 = 0; i2 < array.Length; i2++) {
if (i != i2 && array[i] == array[i2]) {
msg = "Invalid References, " + array[i].name + " is represented more than once.";
return false;
}
}
}
return true;
}
private bool IsChildRecursive(Transform t, Transform parent) {
if (t.parent == parent) return true;
if (t.parent != null) return IsChildRecursive(t.parent, parent);
return false;
}
public bool IsEmpty(bool considerRoot) {
if (considerRoot && root != null) return false;
if (hips != null ||
head != null ||
spine != null ||
chest != null ||
leftUpperArm != null ||
leftLowerArm != null ||
leftHand != null ||
rightUpperArm != null ||
rightLowerArm != null ||
rightHand != null ||
leftUpperLeg != null ||
leftLowerLeg != null ||
leftFoot != null ||
rightUpperLeg != null ||
rightLowerLeg != null ||
rightFoot != null
) return false;
return true;
}
/// <summary>
/// Returns true if the References contain the specified Transform
/// </summary>
public bool Contains(Transform t, bool ignoreRoot = false) {
if (!ignoreRoot && root == t) return true;
if (hips == t) return true;
if (spine == t) return true;
if (chest == t) return true;
if (leftUpperLeg == t) return true;
if (leftLowerLeg == t) return true;
if (leftFoot == t) return true;
if (rightUpperLeg == t) return true;
if (rightLowerLeg == t) return true;
if (rightFoot == t) return true;
if (leftUpperArm == t) return true;
if (leftLowerArm == t) return true;
if (leftHand == t) return true;
if (rightUpperArm == t) return true;
if (rightLowerArm == t) return true;
if (rightHand == t) return true;
if (head == t) return true;
return false;
}
public Transform[] GetRagdollTransforms() {
return new Transform[16] {
hips,
spine,
chest,
head,
leftUpperArm,
leftLowerArm,
leftHand,
rightUpperArm,
rightLowerArm,
rightHand,
leftUpperLeg,
leftLowerLeg,
leftFoot,
rightUpperLeg,
rightLowerLeg,
rightFoot
};
}
public static BipedRagdollReferences FromAvatar(Animator animator) {
BipedRagdollReferences r = new BipedRagdollReferences();
r.root = GameObject.Find("Position").transform;
r.hips = GameObject.Find("Hip").transform;
r.spine = GameObject.Find("Spine").transform;
r.chest = GameObject.Find("Chest").transform;
r.head = GameObject.Find("Head").transform;
r.leftUpperArm = GameObject.Find("Arm_L").transform;
r.leftLowerArm = GameObject.Find("Elbow_L").transform;
r.leftHand = GameObject.Find("Wrist_L").transform;
r.rightUpperArm = GameObject.Find("Arm_R").transform;
r.rightLowerArm = GameObject.Find("Elbow_R").transform;
r.rightHand = GameObject.Find("Wrist_R").transform;
r.leftUpperLeg = GameObject.Find("Thigh_L").transform;
r.leftLowerLeg = GameObject.Find("Knee_L").transform;
r.leftFoot = GameObject.Find("Ankle_L").transform;
r.rightUpperLeg = GameObject.Find("Thigh_R").transform;
r.rightLowerLeg = GameObject.Find("Knee_R").transform;
r.rightFoot = GameObject.Find("Ankle_R").transform;
return r;
}
public static BipedRagdollReferences FromBipedReferences(BipedReferences biped) {
BipedRagdollReferences r = new BipedRagdollReferences();
//r.root = biped.root;
//r.hips = biped.pelvis;
//if (biped.spine != null && biped.spine.Length > 0) {
// r.spine = biped.spine[0];
// if (biped.spine.Length > 1) r.chest = biped.spine[biped.spine.Length - 1];
//}
//r.head = biped.head;
//r.leftUpperArm = biped.leftUpperArm;
//r.leftLowerArm = biped.leftForearm;
//r.leftHand = biped.leftHand;
//r.rightUpperArm = biped.rightUpperArm;
//r.rightLowerArm = biped.rightForearm;
//r.rightHand = biped.rightHand;
//r.leftUpperLeg = biped.leftThigh;
//r.leftLowerLeg = biped.leftCalf;
//r.leftFoot = biped.leftFoot;
//r.rightUpperLeg = biped.rightThigh;
//r.rightLowerLeg = biped.rightCalf;
//r.rightFoot = biped.rightFoot;
r.root = GameObject.Find("Position").transform;
r.hips = GameObject.Find("Hip").transform;
r.spine = GameObject.Find("Spine").transform;
r.chest = GameObject.Find("Chest").transform;
r.head = GameObject.Find("Head").transform;
r.leftUpperArm = GameObject.Find("Arm_L").transform;
r.leftLowerArm = GameObject.Find("Elbow_L").transform;
r.leftHand = GameObject.Find("Wrist_L").transform;
r.rightUpperArm = GameObject.Find("Arm_R").transform;
r.rightLowerArm = GameObject.Find("Elbow_R").transform;
r.rightHand = GameObject.Find("Wrist_R").transform;
r.leftUpperLeg = GameObject.Find("Thigh_L").transform;
r.leftLowerLeg = GameObject.Find("Knee_L").transform;
r.leftFoot = GameObject.Find("Ankle_L").transform;
r.rightUpperLeg = GameObject.Find("Thigh_R").transform;
r.rightLowerLeg = GameObject.Find("Knee_R").transform;
r.rightFoot = GameObject.Find("Ankle_R").transform;
return r;
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a0a96d624395a4a41af2560dbc09832b
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace RootMotion.Dynamics
{
public class DragRig : MonoBehaviour
{
private Rigidbody rig;
private void Start()
{
rig = transform.GetComponent<Rigidbody>();
}
private void OnMouseDrag()
{
Vector3 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition+new Vector3(0,0,1.5f));
rig.velocity = ((cursorPos - transform.position)*8);
rig.angularVelocity = Vector3.zero;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b987d479983ed043b4ae4c265205883
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,251 @@
using UnityEngine;
using System.Collections;
namespace RootMotion.Dynamics {
/// <summary>
/// Contains tools for converting 3D Joints.
/// </summary>
public static class JointConverter {
#region Public
/// <summary>
/// Converts any 3D joints on the root GameObject and it's children to ConfigurableJoints
/// </summary>
/// <param name="root">Root.</param>
public static void ToConfigurable(GameObject root) {
int count = 0;
CharacterJoint[] characterJoints = root.GetComponentsInChildren<CharacterJoint>();
for (int i = 0; i < characterJoints.Length; i++) {
CharacterToConfigurable(characterJoints[i]);
count ++;
}
HingeJoint[] hingeJoints = root.GetComponentsInChildren<HingeJoint>();
for (int i = 0; i < hingeJoints.Length; i++) {
HingeToConfigurable(hingeJoints[i]);
count ++;
}
FixedJoint[] fixedJoints = root.GetComponentsInChildren<FixedJoint>();
for (int i = 0; i < fixedJoints.Length; i++) {
FixedToConfigurable(fixedJoints[i]);
count ++;
}
SpringJoint[] springJoints = root.GetComponentsInChildren<SpringJoint>();
for (int i = 0; i < springJoints.Length; i++) {
SpringToConfigurable(springJoints[i]);
count ++;
}
if (count > 0) {
Debug.Log(count.ToString() + " joints were successfully converted to ConfigurableJoints.");
} else {
Debug.Log("No joints found in the children of " + root.name + " to convert to ConfigurableJoints.");
}
}
/// <summary>
/// Replaces a HingeJoint with a ConfigurableJoint.
/// </summary>
public static void HingeToConfigurable(HingeJoint src) {
#if UNITY_EDITOR
ConfigurableJoint conf = UnityEditor.Undo.AddComponent(src.gameObject, typeof(ConfigurableJoint)) as ConfigurableJoint;
#else
ConfigurableJoint conf = src.gameObject.AddComponent<ConfigurableJoint>();
#endif
ConvertJoint(ref conf, src as Joint);
conf.secondaryAxis = Vector3.zero;
conf.xMotion = ConfigurableJointMotion.Locked;
conf.yMotion = ConfigurableJointMotion.Locked;
conf.zMotion = ConfigurableJointMotion.Locked;
conf.angularXMotion = src.useLimits? ConfigurableJointMotion.Limited: ConfigurableJointMotion.Free;
conf.angularYMotion = ConfigurableJointMotion.Locked;
conf.angularZMotion = ConfigurableJointMotion.Locked;
conf.highAngularXLimit = ConvertToHighSoftJointLimit(src.limits, src.spring, src.useSpring);
conf.angularXLimitSpring = ConvertToSoftJointLimitSpring(src.limits, src.spring, src.useSpring);
conf.lowAngularXLimit = ConvertToLowSoftJointLimit(src.limits, src.spring, src.useSpring);
if (src.useMotor) {
Debug.LogWarning("Can not convert HingeJoint Motor to ConfigurableJoint.");
}
#if UNITY_EDITOR
UnityEditor.Undo.DestroyObjectImmediate(src);
#else
GameObject.DestroyImmediate(src);
#endif
}
/// <summary>
/// Replaces a FixedJoint with a ConfigurableJoint.
/// </summary>
public static void FixedToConfigurable(FixedJoint src) {
#if UNITY_EDITOR
ConfigurableJoint conf = UnityEditor.Undo.AddComponent(src.gameObject, typeof(ConfigurableJoint)) as ConfigurableJoint;
#else
ConfigurableJoint conf = src.gameObject.AddComponent<ConfigurableJoint>();
#endif
ConvertJoint(ref conf, src as Joint);
conf.secondaryAxis = Vector3.zero;
conf.xMotion = ConfigurableJointMotion.Locked;
conf.yMotion = ConfigurableJointMotion.Locked;
conf.zMotion = ConfigurableJointMotion.Locked;
conf.angularXMotion = ConfigurableJointMotion.Locked;
conf.angularYMotion = ConfigurableJointMotion.Locked;
conf.angularZMotion = ConfigurableJointMotion.Locked;
#if UNITY_EDITOR
UnityEditor.Undo.DestroyObjectImmediate(src);
#else
GameObject.DestroyImmediate(src);
#endif
}
/// <summary>
/// Replaces a SpringJoint with a ConfigurableJoint.
/// </summary>
public static void SpringToConfigurable(SpringJoint src) {
#if UNITY_EDITOR
ConfigurableJoint conf = UnityEditor.Undo.AddComponent(src.gameObject, typeof(ConfigurableJoint)) as ConfigurableJoint;
#else
ConfigurableJoint conf = src.gameObject.AddComponent<ConfigurableJoint>();
#endif
ConvertJoint(ref conf, src as Joint);
conf.xMotion = ConfigurableJointMotion.Limited;
conf.yMotion = ConfigurableJointMotion.Limited;
conf.zMotion = ConfigurableJointMotion.Limited;
conf.angularXMotion = ConfigurableJointMotion.Free;
conf.angularYMotion = ConfigurableJointMotion.Free;
conf.angularZMotion = ConfigurableJointMotion.Free;
SoftJointLimit linearLimit = new SoftJointLimit();
linearLimit.bounciness = 0f;
linearLimit.limit = src.maxDistance;
conf.linearLimit = linearLimit;
SoftJointLimitSpring linearLimitSpring = new SoftJointLimitSpring();
linearLimitSpring.damper = src.damper;
linearLimitSpring.spring = src.spring;
conf.linearLimitSpring = linearLimitSpring;
#if UNITY_EDITOR
UnityEditor.Undo.DestroyObjectImmediate(src);
#else
GameObject.DestroyImmediate(src);
#endif
}
/// <summary>
/// Replaces a CharacterJoint with a ConfigurableJoint.
/// </summary>
public static void CharacterToConfigurable(CharacterJoint src) {
#if UNITY_EDITOR
ConfigurableJoint conf = UnityEditor.Undo.AddComponent(src.gameObject, typeof(ConfigurableJoint)) as ConfigurableJoint;
#else
ConfigurableJoint conf = src.gameObject.AddComponent<ConfigurableJoint>();
#endif
ConvertJoint(ref conf, src as Joint);
conf.secondaryAxis = src.swingAxis;
conf.xMotion = ConfigurableJointMotion.Locked;
conf.yMotion = ConfigurableJointMotion.Locked;
conf.zMotion = ConfigurableJointMotion.Locked;
conf.angularXMotion = ConfigurableJointMotion.Limited;
conf.angularYMotion = ConfigurableJointMotion.Limited;
conf.angularZMotion = ConfigurableJointMotion.Limited;
conf.highAngularXLimit = CopyLimit(src.highTwistLimit);
conf.lowAngularXLimit = CopyLimit(src.lowTwistLimit);
conf.angularYLimit = CopyLimit(src.swing1Limit);
conf.angularZLimit = CopyLimit(src.swing2Limit);
conf.angularXLimitSpring = CopyLimitSpring(src.twistLimitSpring);
conf.angularYZLimitSpring = CopyLimitSpring(src.swingLimitSpring);
conf.enableCollision = src.enableCollision;
conf.projectionMode = src.enableProjection? JointProjectionMode.PositionAndRotation: JointProjectionMode.None;
conf.projectionAngle = src.projectionAngle;
conf.projectionDistance = src.projectionDistance;
#if UNITY_EDITOR
UnityEditor.Undo.DestroyObjectImmediate(src);
#else
GameObject.DestroyImmediate(src);
#endif
}
#endregion Public
// Common to all joints
private static void ConvertJoint(ref ConfigurableJoint conf, Joint src) {
conf.anchor = src.anchor;
conf.autoConfigureConnectedAnchor = src.autoConfigureConnectedAnchor;
conf.axis = src.axis;
conf.breakForce = src.breakForce;
conf.breakTorque = src.breakTorque;
conf.connectedAnchor = src.connectedAnchor;
conf.connectedBody = src.connectedBody;
conf.enableCollision = src.enableCollision;
}
// Conversion from JointLimit and JointSpring to high SoftJointLimit
private static SoftJointLimit ConvertToHighSoftJointLimit(JointLimits src, JointSpring spring, bool useSpring) {
SoftJointLimit limit = new SoftJointLimit();
limit.limit = -src.max;
limit.bounciness = src.bounciness;
return limit;
}
// Conversion from JointLimit and JointSpring to low SoftJointLimit
private static SoftJointLimit ConvertToLowSoftJointLimit(JointLimits src, JointSpring spring, bool useSpring) {
SoftJointLimit limit = new SoftJointLimit();
limit.limit = -src.min;
limit.bounciness = src.bounciness;
return limit;
}
// Conversion from JointLimit and JointSpring to SoftJointLimitSpring
private static SoftJointLimitSpring ConvertToSoftJointLimitSpring(JointLimits src, JointSpring spring, bool useSpring) {
SoftJointLimitSpring limitSpring = new SoftJointLimitSpring();
limitSpring.damper = useSpring? spring.damper: 0f;
limitSpring.spring = useSpring? spring.spring: 0f;
return limitSpring;
}
// Returns a copy of the specified SoftJointLimit
private static SoftJointLimit CopyLimit(SoftJointLimit src) {
SoftJointLimit limit = new SoftJointLimit();
limit.limit = src.limit;
limit.bounciness = src.bounciness;
return limit;
}
// Returns a copy of the specified SoftJointLimitSpring
private static SoftJointLimitSpring CopyLimitSpring(SoftJointLimitSpring src) {
SoftJointLimitSpring limitSpring = new SoftJointLimitSpring();
limitSpring.damper = src.damper;
limitSpring.spring = src.spring;
return limitSpring;
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 228e073a918c54b9a985c033ce04b015
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,321 @@
using UnityEngine;
using System.Collections;
using UnityEditor;
namespace RootMotion.Dynamics {
/// <summary>
/// Contains common functionality and helpers for creating any type of ragdolls.
/// </summary>
public abstract class RagdollCreator: MonoBehaviour {
static RagdollCreator instance;
private void Awake()
{
instance = this;
}
[System.Serializable]
public enum ColliderType {
Box,
Capsule
}
[System.Serializable]
public enum JointType {
Configurable,
Character
}
[System.Serializable]
public enum Direction {
X = 0,
Y = 1,
Z = 2
}
public struct CreateJointParams {
public Rigidbody rigidbody;
public Rigidbody connectedBody;
public Transform child;
public Vector3 worldSwingAxis;
public Limits limits;
public JointType type;
public struct Limits {
public float minSwing;
public float maxSwing;
public float swing2;
public float twist;
public Limits (float minSwing, float maxSwing, float swing2, float twist) {
this.minSwing = minSwing;
this.maxSwing = maxSwing;
this.swing2 = swing2;
this.twist = twist;
}
}
public CreateJointParams (Rigidbody rigidbody, Rigidbody connectedBody, Transform child, Vector3 worldSwingAxis, Limits limits, JointType type) {
this.rigidbody = rigidbody;
this.connectedBody = connectedBody;
this.child = child;
this.worldSwingAxis = worldSwingAxis;
this.limits = limits;
this.type = type;
}
}
public static void ClearAll(Transform root) {
if (root == null) return;
// If there is a Humanoid Animator, use it to find the first bone to clear
Transform r = root;
Animator animator = root.GetComponentInChildren<Animator>();
if (animator != null && animator.isHuman) {
// Check if Optimize GameObjects is used
Transform hips = animator.GetBoneTransform(HumanBodyBones.Hips);
if (hips != null) {
Transform[] hipChildren = hips.GetComponentsInChildren<Transform>();
if (hipChildren.Length > 2) {
r = hips;
}
}
}
Transform[] transforms = r.GetComponentsInChildren<Transform>();
if (transforms.Length < 2) return;
for (int i = animator != null && animator.isHuman? 0: 1; i < transforms.Length; i++) {
ClearTransform(transforms[i]);
}
}
protected static void ClearTransform(Transform transform) {
if (transform == null) return;
Collider[] colliders = transform.GetComponents<Collider>();
foreach (Collider collider in colliders) {
if (collider != null && !collider.isTrigger) GameObject.DestroyImmediate(collider);
}
var joint = transform.GetComponent<Joint>();
if (joint != null) GameObject.DestroyImmediate(joint);
var rigidbody = transform.GetComponent<Rigidbody>();
if (rigidbody != null) GameObject.DestroyImmediate(rigidbody);
}
protected static void CreateCollider(Transform t, Vector3 startPoint, Vector3 endPoint, ColliderType colliderType, float lengthOverlap, float width) {
Vector3 direction = endPoint - startPoint;
float height = direction.magnitude * (1f + lengthOverlap);
Vector3 heightAxis = AxisTools.GetAxisVectorToDirection(t, direction);
t.gameObject.AddComponent<Rigidbody>();
t.GetComponent<Rigidbody>().angularDrag = 50;
t.GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate;
if (!t.TryGetComponent<DragRig>(out DragRig y))
{
t.gameObject.AddComponent<DragRig>();
}
float scaleF = GetScaleF(t);
switch(colliderType) {
case ColliderType.Capsule:
CapsuleCollider capsule = t.gameObject.AddComponent<CapsuleCollider>();
capsule.height = Mathf.Abs(height / scaleF);
capsule.radius = Mathf.Abs((width * 0.75f) / scaleF);
capsule.direction = DirectionVector3ToInt(heightAxis);
capsule.center = t.InverseTransformPoint(Vector3.Lerp(startPoint, endPoint, 0.5f));
break;
case ColliderType.Box:
Vector3 size = Vector3.Scale(heightAxis, new Vector3(height, height, height));
if (size.x == 0f) size.x = width;
if (size.y == 0f) size.y = width;
if (size.z == 0f) size.z = width;
BoxCollider box = t.gameObject.AddComponent<BoxCollider>();
box.size = size / scaleF;
box.size = new Vector3(Mathf.Abs(box.size.x), Mathf.Abs(box.size.y), Mathf.Abs(box.size.z));
box.center = t.InverseTransformPoint(Vector3.Lerp(startPoint, endPoint, 0.5f));
break;
}
}
protected static void CreateCollider(Transform t, Vector3 startPoint, Vector3 endPoint, ColliderType colliderType, float lengthOverlap, float width, float proportionAspect, Vector3 widthDirection) {
if (colliderType == ColliderType.Capsule) {
CreateCollider(t, startPoint, endPoint, colliderType, lengthOverlap, width * proportionAspect);
return;
}
Vector3 direction = endPoint - startPoint;
float height = direction.magnitude * (1f + lengthOverlap);
Vector3 heightAxis = AxisTools.GetAxisVectorToDirection(t, direction);
Vector3 widthAxis = AxisTools.GetAxisVectorToDirection(t, widthDirection);
if (widthAxis == heightAxis) {
Debug.LogWarning("Width axis = height axis on " + t.name, t);
widthAxis = new Vector3(heightAxis.y, heightAxis.z, heightAxis.x);
}
t.gameObject.AddComponent<Rigidbody>();
t.GetComponent<Rigidbody>().angularDrag = 50;
t.GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate;
if (!t.TryGetComponent<DragRig>(out DragRig y))
{
t.gameObject.AddComponent<DragRig>();
}
Vector3 heightAdd = Vector3.Scale(heightAxis, new Vector3(height, height, height));
Vector3 widthAdd = Vector3.Scale(widthAxis, new Vector3(width, width, width));
Vector3 size = heightAdd + widthAdd;
if (size.x == 0f) size.x = width * proportionAspect;
if (size.y == 0f) size.y = width * proportionAspect;
if (size.z == 0f) size.z = width * proportionAspect;
BoxCollider box = t.gameObject.AddComponent<BoxCollider>();
box.size = size / GetScaleF(t);
box.center = t.InverseTransformPoint(Vector3.Lerp(startPoint, endPoint, 0.5f));
}
protected static float GetScaleF(Transform t) {
Vector3 scale = t.lossyScale;
return (scale.x + scale.y + scale.z) / 3f;
}
protected static Vector3 Abs(Vector3 v) {
Vector3Abs(ref v);
return v;
}
protected static void Vector3Abs(ref Vector3 v) {
v.x = Mathf.Abs(v.x);
v.y = Mathf.Abs(v.y);
v.z = Mathf.Abs(v.z);
}
protected static Vector3 DirectionIntToVector3(int dir) {
if (dir == 0) return Vector3.right;
if (dir == 1) return Vector3.up;
return Vector3.forward;
}
protected static Vector3 DirectionToVector3(Direction dir) {
if (dir == Direction.X) return Vector3.right;
if (dir == Direction.Y) return Vector3.up;
return Vector3.forward;
}
protected static int DirectionVector3ToInt(Vector3 dir) {
float dotX = Vector3.Dot(dir, Vector3.right);
float dotY = Vector3.Dot(dir, Vector3.up);
float dotZ = Vector3.Dot(dir, Vector3.forward);
float absDotX = Mathf.Abs(dotX);
float absDotY = Mathf.Abs(dotY);
float absDotZ = Mathf.Abs(dotZ);
int rotatedDirection = 0;
if (absDotY > absDotX && absDotY > absDotZ) rotatedDirection = 1;
if (absDotZ > absDotX && absDotZ > absDotY) rotatedDirection = 2;
return rotatedDirection;
}
protected static Vector3 GetLocalOrthoDirection(Transform transform, Vector3 worldDir) {
worldDir = worldDir.normalized;
float dotX = Vector3.Dot(worldDir, transform.right);
float dotY = Vector3.Dot(worldDir, transform.up);
float dotZ = Vector3.Dot(worldDir, transform.forward);
float absDotX = Mathf.Abs(dotX);
float absDotY = Mathf.Abs(dotY);
float absDotZ = Mathf.Abs(dotZ);
Vector3 orthoDirection = Vector3.right;
if (absDotY > absDotX && absDotY > absDotZ) orthoDirection = Vector3.up;
if (absDotZ > absDotX && absDotZ > absDotY) orthoDirection = Vector3.forward;
if (Vector3.Dot(worldDir, transform.rotation * orthoDirection) < 0f) orthoDirection = -orthoDirection;
return orthoDirection;
}
protected static Rigidbody GetConnectedBody(Transform bone, ref Transform[] bones) {
if (bone.parent == null) return null;
foreach (Transform bone2 in bones) {
if (bone.parent == bone2 && bone2.GetComponent<Rigidbody>() != null) return bone2.GetComponent<Rigidbody>();
}
return GetConnectedBody(bone.parent, ref bones);
}
protected static void CreateJoint(CreateJointParams p) {
Vector3 axis = GetLocalOrthoDirection(p.rigidbody.transform, p.worldSwingAxis);
Vector3 twistAxis = Vector3.forward;
if (p.child != null) {
twistAxis = GetLocalOrthoDirection(p.rigidbody.transform, p.child.position - p.rigidbody.transform.position);
} else if (p.connectedBody != null) {
twistAxis = GetLocalOrthoDirection(p.rigidbody.transform, p.rigidbody.transform.position - p.connectedBody.transform.position);
}
Vector3 secondaryAxis = Vector3.Cross(axis, twistAxis);
if (p.type == JointType.Configurable) {
ConfigurableJoint j = p.rigidbody.gameObject.AddComponent<ConfigurableJoint>();
j.connectedBody = p.connectedBody;
ConfigurableJointMotion linearMotion = p.connectedBody != null? ConfigurableJointMotion.Locked: ConfigurableJointMotion.Free;
ConfigurableJointMotion angularMotion = p.connectedBody != null? ConfigurableJointMotion.Limited: ConfigurableJointMotion.Free;
j.xMotion = linearMotion;
j.yMotion = linearMotion;
j.zMotion = linearMotion;
j.angularXMotion = angularMotion;
j.angularYMotion = angularMotion;
j.angularZMotion = angularMotion;
if (p.connectedBody != null) {
j.axis = axis;
j.secondaryAxis = secondaryAxis;
j.lowAngularXLimit = ToSoftJointLimit(p.limits.minSwing);
j.highAngularXLimit = ToSoftJointLimit(p.limits.maxSwing);
j.angularYLimit = ToSoftJointLimit(p.limits.swing2);
j.angularZLimit = ToSoftJointLimit(p.limits.twist);
}
j.anchor = Vector3.zero;
} else {
if (p.connectedBody == null) return;
CharacterJoint j = p.rigidbody.gameObject.AddComponent<CharacterJoint>();
j.connectedBody = p.connectedBody;
j.axis = axis;
j.swingAxis = secondaryAxis;
j.lowTwistLimit = ToSoftJointLimit(p.limits.minSwing);
j.highTwistLimit = ToSoftJointLimit(p.limits.maxSwing);
j.swing1Limit = ToSoftJointLimit(p.limits.swing2);
j.swing2Limit = ToSoftJointLimit(p.limits.twist);
j.anchor = Vector3.zero;
}
}
private static SoftJointLimit ToSoftJointLimit(float limit) {
SoftJointLimit s = new SoftJointLimit();
s.limit = limit;
return s;
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1b9a4b5cc2aac429c8cac984f2f44e2d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,43 @@
using UnityEngine;
using System.Collections;
namespace RootMotion.Dynamics {
/// <summary>
/// Interactive Scene View ragdoll editor.
/// </summary>
[HelpURL("https://www.youtube.com/watch?v=y-luLRVmL7E&index=1&list=PLVxSIA1OaTOuE2SB9NUbckQ9r2hTg4mvL")]
[AddComponentMenu("Scripts/RootMotion.Dynamics/Ragdoll Manager/Ragdoll Editor")]
public class RagdollEditor : MonoBehaviour {
// Open the User Manual URL
[ContextMenu("User Manual")]
void OpenUserManual() {
Application.OpenURL("http://root-motion.com/puppetmasterdox/html/page2.html");
}
// Open the Script Reference URL
[ContextMenu("Scrpt Reference")]
void OpenScriptReference() {
Application.OpenURL("http://root-motion.com/puppetmasterdox/html/class_root_motion_1_1_dynamics_1_1_ragdoll_editor.html");
}
// Open a video tutorial about setting up the component
[ContextMenu("TUTORIAL VIDEO")]
void OpenTutorial() {
Application.OpenURL("https://www.youtube.com/watch?v=y-luLRVmL7E&index=1&list=PLVxSIA1OaTOuE2SB9NUbckQ9r2hTg4mvL");
}
[System.Serializable]
public enum Mode {
Colliders,
Joints
}
[HideInInspector] public Rigidbody selectedRigidbody;
[HideInInspector] public Collider selectedCollider;
[HideInInspector] public bool symmetry = true;
[HideInInspector] public Mode mode;
}
}
@@ -0,0 +1,16 @@
fileFormatVersion: 2
guid: 4d25a140bfa1c49bfafcaa11cff00f2a
labels:
- Ragdoll
- Editor
- Joints
timeCreated: 1444739198
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 07d937e4fdaf46a498a8bfc405e39ccf, type: 3}
userData:
assetBundleName:
assetBundleVariant: