コード例 #1
0
        private void OnCollisionStay(Collision collision)
        {
            if (!ready)
            {
                return;
            }

            var other = collision.gameObject;

            if (other.GetComponent <Rigidbody>().isKinematic &&
                objRigidbody.isKinematic)
            {
                return;
            }

            // Solve collision and set corresponding gains
            SolveCollisionVertices(collision);

            float maxSpeed, currentSpeed;

            if (!objRigidbody.isKinematic)
            {
                // Check if only transient contanct
                if (!OnGround)
                {
                    return;
                }

                currentSpeed = CurrentVelocity.magnitude;
                maxSpeed     = MaxSpeed;
            }
            else
            {
                CollisionHandler otherCollisionHandler =
                    other.GetComponent <CollisionHandler>();
                currentSpeed =
                    otherCollisionHandler.CurrentVelocity.magnitude;
                maxSpeed =
                    otherCollisionHandler.MaxSpeed;
            }

            if (currentSpeed > 0.1f)
            {
                friction.SetLevel(FrictionGainDb);
            }

            float freqDelta =
                (currentSpeed / MaxSpeed) * Time.deltaTime;
            float freqScale =
                Mathf.MoveTowards(currentSpeed, MaxSpeed, freqDelta);
            float freqPct =
                Mathf.Clamp01(freqScale / MaxSpeed);

            friction.SetFrequencyPct(freqPct * MaxFreqPct);
        }
コード例 #2
0
        private void OnCollisionEnter(Collision collision)
        {
            if (!ready)
            {
                return;
            }

            var other = collision.gameObject;

            if (other.GetComponent <Rigidbody>().isKinematic &&
                objRigidbody.isKinematic)
            {
                return;
            }

            // Solve collision and set corresponding gains
            SolveCollisionVertices(collision);

            // Add an impact to to the impact generator
            float maxSpeed;

            if (objRigidbody.isKinematic)
            {
                CollisionHandler otherCollisionHandler =
                    other.GetComponent <CollisionHandler>();
                maxSpeed = otherCollisionHandler.MaxSpeed;
            }
            else
            {
                maxSpeed = MaxSpeed;
            }

            Vector3 relativeVelocity =
                collision.relativeVelocity;

            relativeVelocity =
                Vector3.ClampMagnitude(relativeVelocity, maxSpeed);

            float impactMag       = relativeVelocity.magnitude;
            float linearImpactMag = impactMag / maxSpeed;
            float impactDuration  =
                Mathf.Lerp(MINIMPACTDURATION, MAXIMPACTDURATION, linearImpactMag);

            softImpact.Hit(impactDuration, ImpactGain * impactMag);
        }