Exemplo n.º 1
0
            public Leg(IKSolverFullBodyBiped solver, PuppetMaster puppetMaster, FullBodyBipedEffector effectorType)
            {
                this.solver = solver;
                effector    = solver.GetEffector(effectorType);
                chain       = solver.GetChain(effectorType);

                stepFrom = effector.bone.position;
                stepTo   = effector.bone.position;
                position = effector.bone.position;

                offset = effectorType == FullBodyBipedEffector.LeftFoot? Vector3.left: Vector3.right;

                stepTimer  = 0f;
                stepLength = 0f;

                muscle      = puppetMaster.GetMuscle(effector.bone);
                thighMuscle = puppetMaster.GetMuscle(chain.nodes[0].transform);

                mass          = muscle.rigidbody.mass;
                inertiaTensor = muscle.rigidbody.inertiaTensor;
            }
Exemplo n.º 2
0
        public static bool AddPropMuscle(PuppetMaster script, ConfigurableJoint addPropMuscleTo, Vector3 position, Quaternion rotation, Vector3 additionalPinOffset, Transform targetParent = null)
        {
            var serializedObject = new SerializedObject(script);

            if (addPropMuscleTo != null)
            {
                bool isFlat = script.HierarchyIsFlat();

                var addToMuscle = script.GetMuscle(addPropMuscleTo);
                if (addToMuscle != null)
                {
                    GameObject go = new GameObject("Prop Muscle " + addPropMuscleTo.name);
                    go.layer              = addPropMuscleTo.gameObject.layer;
                    go.transform.parent   = isFlat ? script.transform : addPropMuscleTo.transform;
                    go.transform.position = position;
                    go.transform.rotation = rotation;

                    var r = go.AddComponent <Rigidbody>();

                    GameObject target = new GameObject("Prop Muscle Target " + addPropMuscleTo.name);
                    target.gameObject.layer   = addToMuscle.target.gameObject.layer;
                    target.transform.parent   = targetParent == null ? addToMuscle.target : targetParent;
                    target.transform.position = go.transform.position;
                    target.transform.rotation = go.transform.rotation;

                    var joint = go.AddComponent <ConfigurableJoint>();
                    joint.connectedBody  = addToMuscle.joint.gameObject.GetComponent <Rigidbody>();
                    joint.xMotion        = ConfigurableJointMotion.Locked;
                    joint.yMotion        = ConfigurableJointMotion.Locked;
                    joint.zMotion        = ConfigurableJointMotion.Locked;
                    joint.angularXMotion = ConfigurableJointMotion.Locked;
                    joint.angularYMotion = ConfigurableJointMotion.Locked;
                    joint.angularZMotion = ConfigurableJointMotion.Locked;

                    r.interpolation = joint.connectedBody.interpolation;

                    int newLength      = script.muscles.Length + 1;
                    int newMuscleIndex = newLength - 1;

                    SerializedProperty muscles = serializedObject.FindProperty("muscles");
                    muscles.InsertArrayElementAtIndex(newMuscleIndex);

                    muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("index").intValue              = newMuscleIndex;
                    muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("joint").objectReferenceValue  = joint;
                    muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("target").objectReferenceValue = target.transform;
                    muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("props").FindPropertyRelative("group").intValue = 8;
                    muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("isPropMuscle").boolValue = true;
                    serializedObject.ApplyModifiedProperties();

                    var propMuscle  = joint.gameObject.AddComponent <PropMuscle>();
                    var propMuscleS = new SerializedObject(propMuscle);
                    propMuscleS.FindProperty("puppetMaster").objectReferenceValue = script;
                    propMuscleS.FindProperty("additionalPinOffset").vector3Value  = additionalPinOffset;

                    propMuscleS.ApplyModifiedProperties();

                    if (additionalPinOffset != Vector3.zero)
                    {
                        propMuscleS.Update();
                        ConfigurableJoint additionalJoint  = null;
                        Transform         additionalTarget = null;
                        PropMuscleInspector.AddAdditionalPinUnserialized(propMuscle, out additionalJoint, out additionalTarget);

                        muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("additionalPin").objectReferenceValue       = additionalJoint;
                        muscles.GetArrayElementAtIndex(newMuscleIndex).FindPropertyRelative("additionalPinTarget").objectReferenceValue = additionalTarget;

                        propMuscleS.ApplyModifiedProperties();
                    }

                    serializedObject.ApplyModifiedProperties();
                    // TODO Do not add if addPropMuscleTo already has a PropMuscle

                    return(true);
                }
                else
                {
                    Debug.LogError("Can't add Prop Muscle to a ConfigurableJoint that is not in the list of PuppetMaster.muscles.", script.transform);
                    return(false);
                }
            }
            else
            {
                Debug.LogError("Please assign the ConfigurableJoint of the muscle you wish to add the Prop Muscle to.", script.transform);
                return(false);
            }
        }