Exemplo n.º 1
0
        private void ApplyInputStrafe()
        {
            if (!strafeStick.IsZero())
            {
                // var currentVelocity = new Vector3(PodBody.velocity.x, 0, PodBody.velocity.z);
                // var orthoForward = transform.forward.Planar(Vector3.up);
                var   currentHorVelocity   = Vector3.Dot(PodBody.velocity, right);
                var   currentForVelocity   = Vector3.Dot(PodBody.velocity, forward);
                var   currentVelocity      = currentHorVelocity * right + currentForVelocity * forward;
                var   currentPushDirection = Quaternion.AngleAxis(Vector2.up.GetClockwiseAngle(strafeStick.normalized), up) * forward;
                float percentPower         = strafeStick.magnitude;

                if (showStrafeInput)
                {
                    Debug.DrawRay(vehicleBounds.center, currentPushDirection * percentPower * 5, Color.white);
                }
                // float currentSpeed = currentVelocity.magnitude * currentVelocity.normalized.PercentDirection(currentPushDirection);
                float   currentSpeed      = Vector3.Dot(currentVelocity, currentPushDirection);
                float   inputAcceleration = percentPower * acceleration;
                float   desiredSpeed      = Mathf.Clamp(currentSpeed + inputAcceleration, -maxSpeed, maxSpeed);
                Vector3 appliedForce      = PhysicsHelpers.CalculateRequiredForceForSpeed(PodBody.mass, currentSpeed, desiredSpeed) * currentPushDirection;
                // appliedForce = new Vector3(appliedForce.x, 0, appliedForce.z);
                appliedForce -= up * Vector3.Dot(appliedForce, up);
                PodBody.AddForce(appliedForce, ForceMode.Force);
            }
        }
Exemplo n.º 2
0
        private void CalculateOrientationFromSurroundings()
        {
            var rayResults = PhysicsHelpers.CastRays(vehicleBounds.center, showOrientationCasters, 15, 15, 10, groundMask, Space.World);
            var rayHits    = rayResults.Where(rayResult => rayResult.raycastHit);

            var nextUp = Vector3.up;

            if (rayHits.Count() > 0)
            {
                nextUp = (rayHits.Select(rayInfo => rayInfo.hitData[0].normal).Aggregate((firstRay, secondRay) => firstRay + secondRay) / rayHits.Count()).normalized;
            }
            up                = Vector3.Lerp(up, nextUp, Time.fixedDeltaTime * 5);
            forward           = Vector3.ProjectOnPlane(transform.forward, up).normalized;
            right             = Quaternion.AngleAxis(90, up) * forward;
            castedOrientation = Quaternion.LookRotation(forward, up);

            if (showCalculatedUp)
            {
                Debug.DrawRay(vehicleBounds.center, up * 5, Color.green);
            }
            if (showCalculatedForward)
            {
                Debug.DrawRay(vehicleBounds.center, forward * 5, Color.blue);
            }
            if (showCalculatedRight)
            {
                Debug.DrawRay(vehicleBounds.center, right * 5, Color.red);
            }
        }
Exemplo n.º 3
0
        void FixedUpdate()
        {
            float acceleration  = GetAcceleration();
            float brakeleration = GetBrakeleration();
            float grip          = GetGrip();

            Vector3 vehicleProjectedForward = vehicleRigidbody.transform.forward.Planar(Vector3.up);

            //float forwardPercent = vehicleRigidbody.velocity.PercentDirection(vehicleProjectedForward);
            currentTotalSpeed = vehicleRigidbody.velocity.magnitude;
            //currentForwardSpeed = currentTotalSpeed * forwardPercent;
            Vector3 planarVelocityVector = vehicleRigidbody.velocity.Planar(Vector3.up);
            float   direction            = Mathf.Sign(planarVelocityVector.normalized.PercentDirection(vehicleProjectedForward));

            currentForwardSpeed = planarVelocityVector.magnitude * direction;
            float currentSpeedPercent = Mathf.Abs(currentForwardSpeed / vehicleStats.maxForwardSpeed);

            float currentMaxWheelAngle = vehicleStats.wheelAngleCurve.Evaluate(currentSpeedPercent) * Mathf.Abs(vehicleStats.slowWheelAngle - vehicleStats.fastWheelAngle) + Mathf.Min(vehicleStats.slowWheelAngle, vehicleStats.fastWheelAngle);
            //Debug.Log(currentMaxWheelAngle);
            Quaternion wheelRotation = Quaternion.Euler(0, currentMaxWheelAngle * steer, 0);

            wheelFL.transform.localRotation = wheelRotation;
            wheelFR.transform.localRotation = wheelRotation;

            wheelFL.SetGrip(grip);
            wheelFR.SetGrip(grip);
            wheelRL.SetGrip(grip);
            wheelRR.SetGrip(grip);

            //if (wheelRL.IsGrounded(-wheelRL.up, wheelGroundDistance) || wheelRR.IsGrounded(-wheelRR.up, wheelGroundDistance))
            if (wheelRL.IsGrounded() || wheelRR.IsGrounded())
            {
                gas   = Mathf.Clamp(gas, -1, 1);
                brake = Mathf.Clamp(brake, 0, 1);
                steer = Mathf.Clamp(steer, -1, 1);

                float gasAmount         = gas * (acceleration + (gas > 0 && currentForwardSpeed < 0 || gas < 0 && currentForwardSpeed > 0 ? brakeleration : 0));
                float brakeAmount       = Mathf.Clamp01(brake + Mathf.Abs(steer) * vehicleStats.percentSteerEffectsBrake.Evaluate(currentSpeedPercent)) * brakeleration * (currentForwardSpeed >= 0 ? -1 : 1);
                float totalAcceleration = gasAmount + brakeAmount;
                if (totalAcceleration > -float.Epsilon && totalAcceleration < float.Epsilon && !(strivedSpeed > -float.Epsilon && strivedSpeed < float.Epsilon))
                {
                    totalAcceleration = vehicleStats.deceleration * (currentForwardSpeed >= 0 ? -1 : 1);
                }
                float deltaSpeed = totalAcceleration * Time.fixedDeltaTime;

                SetStrivedSpeed(strivedSpeed + deltaSpeed);

                float nextCurrentSpeed = currentForwardSpeed + deltaSpeed;
                //If there is too high a difference between the strived speed and the expected next speed then set strived speed to the expected
                if (Mathf.Abs(strivedSpeed - nextCurrentSpeed) > 0)
                {
                    SetStrivedSpeed(nextCurrentSpeed);
                }

                vehicleRigidbody.AddForce(PhysicsHelpers.CalculateRequiredForceForSpeed(vehicleRigidbody.mass, currentForwardSpeed * vehicleRigidbody.transform.forward, strivedSpeed * vehicleRigidbody.transform.forward), ForceMode.Force);
            }

            prevCurrentSpeed = currentForwardSpeed;
            prevStrivedSpeed = strivedSpeed;
            //prevVelocity = vehicleRigidbody.velocity;
        }