예제 #1
0
        public void InitializeSprings(Transform frontLeftPosition, Transform frontRightPosition, Transform rearLeftPosition, Transform rearRightPosition)
        {
            if (springsInitialized)
            {
                throw new InvalidOperationException("Springs already initialized");
            }

            springs    = new Spring2[4];
            springs[0] = new Spring2(frontLeftPosition);
            springs[1] = new Spring2(frontRightPosition);
            springs[2] = new Spring2(rearLeftPosition);
            springs[3] = new Spring2(rearRightPosition);

            springsInitialized = true;
        }
예제 #2
0
        void GetGround(Spring2 spring)
        {
            Vector3    downwards = spring.transform.TransformDirection(-Vector3.up);
            RaycastHit hit;

            // down = local downwards direction
            Vector3 down = spring.transform.TransformDirection(Vector3.down);

            if (Physics.Raycast(spring.transform.position, downwards, out hit, radius + maxSuspension))
            {
                // the velocity at point of contact
                Vector3 velocityAtTouch = rb.GetPointVelocity(hit.point);

                // calculate spring compression
                // difference in positions divided by total suspension range
                float compression = hit.distance / (maxSuspension + radius);
                compression = -compression + 1;

                // final force
                Vector3 force = -downwards * compression * springy;
                // velocity at point of contact transformed into local space

                Vector3 t = spring.transform.InverseTransformDirection(velocityAtTouch);

                // local x and z directions = 0
                t.z = t.x = 0;

                // back to world space * -damping
                Vector3 damping    = spring.transform.TransformDirection(t) * -damper;
                Vector3 finalForce = force + damping;

                rb.AddForceAtPosition(finalForce, hit.point);

                //if (graphic) graphic.position = transform.position + (down * (hit.distance - radius));
            }
            else
            {
                //if (graphic) graphic.position = transform.position + (down * maxSuspension);
            }
        }