예제 #1
0
		// When a muscle collides with something (called by the MuscleCollisionBroadcaster component on the muscle).
		protected override void OnMuscleCollisionBehaviour(MuscleCollision m) {
			// All the conditions for ignoring this
			if (!enabled) return;
			if (state == State.Unpinned) return;
			if (collisions > maxCollisions) return;
			if (!LayerMaskExtensions.Contains(collisionLayers, m.collision.gameObject.layer)) return;
			if (masterProps.normalMode == NormalMode.Kinematic && !puppetMaster.isActive && !masterProps.activateOnStaticCollisions && m.collision.gameObject.isStatic) return;

			// Get the collision impulse on the muscle
			float impulse = GetImpulse(m);
			if (impulse <= 0f) return;
			collisions ++;

			// Try to find out if it collided with another puppet's muscle
			if (m.collision.collider.attachedRigidbody != null) {	
				broadcaster = m.collision.collider.attachedRigidbody.GetComponent<MuscleCollisionBroadcaster>();
				if (broadcaster != null) {
					if (broadcaster.muscleIndex < broadcaster.puppetMaster.muscles.Length) {
						// Multiply impulse (if the other muscle has been boosted)
						impulse *= broadcaster.puppetMaster.muscles[broadcaster.muscleIndex].state.impulseMlp;

						float stayF = m.isStay? 0.05f: 0.1f;
						broadcaster.puppetMaster.muscles[broadcaster.muscleIndex].offset -= m.collision.impulse * Time.deltaTime * stayF;

						/*
						float velocityF = puppetMaster.muscles[m.muscleIndex].rigidbody.velocity.sqrMagnitude / broadcaster.puppetMaster.muscles[broadcaster.muscleIndex].rigidbody.velocity.sqrMagnitude;
						velocityF = Mathf.Clamp(velocityF, 0.5f, 2f);
						//velocityF = 1f + (velocityF - 1f) * 0.5f;
						impulse /= velocityF;
						*/
					}
				}
			}

			// Should we activate the puppet?
			if (Activate(m.collision, impulse)) puppetMaster.mode = PuppetMaster.Mode.Active;

			// Let other scripts know about the collision
			if (OnCollisionImpulse != null) OnCollisionImpulse(m, impulse);

			// Unpin the muscle (and other muscles)
			UnPin(m.muscleIndex, impulse);
		}
예제 #2
0
        // When a muscle collides with something (called by the MuscleCollisionBroadcaster component on the muscle).
        protected override void OnMuscleCollisionBehaviour(MuscleCollision m)
        {
            if (OnCollision != null)
            {
                OnCollision(m);
            }

            // All the conditions for ignoring this
            if (!enabled)
            {
                return;
            }
            if (state == State.Unpinned)
            {
                return;
            }
            if (collisions > maxCollisions)
            {
                return;
            }
            if (!LayerMaskExtensions.Contains(collisionLayers, m.collision.gameObject.layer))
            {
                return;
            }

            if (LayerMaskExtensions.Contains(groundLayers, m.collision.gameObject.layer))
            {
                if (state == State.GetUp)
                {
                    return;                       // Do not damage if contact with ground layers and in getup state
                }
                if (puppetMaster.muscles[m.muscleIndex].props.group == Muscle.Group.Foot)
                {
                    return;                                                                       // Do not damage if feet in contact with ground layers
                }
            }
            if (masterProps.normalMode == NormalMode.Kinematic && !puppetMaster.isActive && !masterProps.activateOnStaticCollisions && m.collision.gameObject.isStatic)
            {
                return;
            }

            // Activate on Kinematic-Kinematic pair
            if (puppetMaster.muscles[m.muscleIndex].rigidbody.isKinematic && m.collision.collider.attachedRigidbody != null && m.collision.collider.attachedRigidbody.isKinematic)
            {
                if (masterProps.normalMode == NormalMode.Kinematic && puppetMaster.mode == PuppetMaster.Mode.Kinematic)
                {
                    puppetMaster.mode = PuppetMaster.Mode.Active;
                }
            }
            else
            {
                // Get the collision impulse on the muscle
                float cT      = collisionThreshold;
                float impulse = GetImpulse(m, ref cT);

                float minImpulseMlp = PuppetMasterSettings.instance != null ? (1f + PuppetMasterSettings.instance.currentlyActivePuppets * PuppetMasterSettings.instance.activePuppetCollisionThresholdMlp) : 1f;
                float minImpulse    = cT * minImpulseMlp;

                if (impulse <= minImpulse)
                {
                    return;
                }
                collisions++;

                // Try to find out if it collided with another puppet's muscle
                if (m.collision.collider.attachedRigidbody != null)
                {
                    broadcaster = m.collision.collider.attachedRigidbody.GetComponent <MuscleCollisionBroadcaster>();
                    if (broadcaster != null)
                    {
                        if (broadcaster.muscleIndex < broadcaster.puppetMaster.muscles.Length)
                        {
                            // Multiply impulse (if the other muscle has been boosted)
                            impulse *= broadcaster.puppetMaster.muscles[broadcaster.muscleIndex].state.impulseMlp;

                            //float stayF = m.isStay? 0.05f: 0.1f;
                            //broadcaster.puppetMaster.muscles[broadcaster.muscleIndex].offset -= m.collision.impulse * Time.deltaTime * stayF;
                        }
                    }
                }

                // DO not move this up, the impulse value will be wrong.
                // Let other scripts know about the collision (even the ones below collision threshold)
                if (OnCollisionImpulse != null)
                {
                    OnCollisionImpulse(m, impulse);
                }

                // Should we activate the puppet?
                if (Activate(m.collision, impulse))
                {
                    puppetMaster.mode = PuppetMaster.Mode.Active;
                }

                // Unpin the muscle (and other muscles)
                UnPin(m.muscleIndex, impulse);
            }
        }