Exemple #1
0
        private void ReconnectMuscle(Muscle m)
        {
            m.state.isDisconnected = false;

            if (activeState != State.Frozen && !m.isPropMuscle)
            {
                m.target.position = m.targetAnimatedPosition;
                m.target.rotation = m.targetAnimatedWorldRotation;
            }

            if (m != muscles[0])
            {
                m.joint.xMotion = ConfigurableJointMotion.Locked;
                m.joint.yMotion = ConfigurableJointMotion.Locked;
                m.joint.zMotion = ConfigurableJointMotion.Locked;

                if (!hierarchyIsFlat && m.joint.connectedBody != null)
                {
                    m.transform.parent = m.joint.connectedBody.transform;
                }
            }

            bool disable = false;

            if (m.joint.connectedBody != null && !m.joint.connectedBody.gameObject.activeInHierarchy)
            {
                disable = true;
            }
            if (m.joint.connectedBody == null)
            {
                if (activeMode == Mode.Disabled || activeState == State.Frozen)
                {
                    disable = true;
                }
            }

            if (disable)
            {
                m.joint.gameObject.SetActive(false);
            }
            else
            {
                if (!m.joint.gameObject.activeInHierarchy || m.state.resetFlag)
                {
                    m.Reset();
                    m.joint.gameObject.SetActive(true);
                }
                else
                {
                    if (activeState != State.Frozen)
                    {
                        m.MoveToTarget();
                    }
                }
            }

            if (activeMode == Mode.Kinematic)
            {
                m.SetKinematic(true);
            }

            if (activeState == State.Dead)
            {
                m.ResetTargetLocalPosition();
                m.SetMuscleRotation(muscleWeight * stateSettings.deadMuscleWeight, muscleSpring, muscleDamper + stateSettings.deadMuscleDamper);
            }

            m.state.resetFlag = false;
            m.ClearVelocities();

            m.state.pinWeightMlp     = 1;
            m.state.muscleWeightMlp  = 1;
            m.state.muscleDamperMlp  = 1;
            m.state.maxForceMlp      = 1;
            m.state.mappingWeightMlp = 1f;

            UpdateInternalCollisions(m);
            m.IgnoreAngularLimits(!angularLimits);

            foreach (BehaviourBase b in behaviours)
            {
                b.OnMuscleReconnected(m);
            }
            if (OnMuscleReconnected != null)
            {
                OnMuscleReconnected(m);
            }
        }
Exemple #2
0
        private void DisconnectMuscle(Muscle m, bool sever, bool deactivate)
        {
            m.state.pinWeightMlp     = 0f;
            m.state.muscleWeightMlp  = 0f;
            m.state.muscleDamperAdd  = 0f;
            m.state.muscleDamperMlp  = 0f;
            m.state.mappingWeightMlp = 0f;
            m.state.maxForceMlp      = 0f;
            m.state.immunity         = 0f;
            m.state.impulseMlp       = 1f;

            if (sever)
            {
                m.joint.xMotion = ConfigurableJointMotion.Free;
                m.joint.yMotion = ConfigurableJointMotion.Free;
                m.joint.zMotion = ConfigurableJointMotion.Free;

                m.IgnoreAngularLimits(true);

                if (!hierarchyIsFlat)
                {
                    m.joint.transform.parent = transform;
                }
            }
            else
            {
                m.IgnoreAngularLimits(false);
            }

            bool applyMappedVelocity = !m.joint.gameObject.activeInHierarchy || m.rigidbody.isKinematic;

            if (activeState == State.Frozen)
            {
                applyMappedVelocity = false;
            }

            // In case disconnecting in disabled mode
            if (!m.joint.gameObject.activeInHierarchy && !deactivate)
            {
                m.MoveToTarget();
                m.joint.gameObject.SetActive(true);
            }

            m.SetKinematic(false);
            JointDrive slerpDrive = new JointDrive();

            slerpDrive.positionSpring = 0f;
            slerpDrive.maximumForce   = 0f;
            slerpDrive.positionDamper = 0f;
            m.joint.slerpDrive        = slerpDrive;

            // Enable internal collisions with the disconnected muscle
            if (!deactivate)
            {
                for (int i = 0; i < muscles.Length; i++)
                {
                    if (muscles[i] != m && !muscles[i].state.isDisconnected)
                    {
                        foreach (Collider c1 in m.colliders)
                        {
                            foreach (Collider c2 in muscles[i].colliders)
                            {
                                if (c1.enabled && c2.enabled)
                                {
                                    Physics.IgnoreCollision(c1, c2, false);
                                }
                            }
                        }
                    }
                }

                if (applyMappedVelocity)
                {
                    m.rigidbody.velocity        = m.mappedVelocity;
                    m.rigidbody.angularVelocity = m.mappedAngularVelocity;
                }
            }
            else
            {
                m.joint.gameObject.SetActive(false);
            }

            if (m.isPropMuscle)
            {
                var propMuscle = m.joint.GetComponent <PropMuscle>();
                if (propMuscle.activeProp != null)
                {
                    propMuscle.currentProp = null;
                }
            }

            m.state.isDisconnected = true;

            foreach (BehaviourBase b in behaviours)
            {
                b.OnMuscleDisconnected(m);
            }
            if (OnMuscleDisconnected != null)
            {
                OnMuscleDisconnected(m);
            }
        }