Ejemplo n.º 1
0
        void DoUpdate()
        {
            // Steering
            Vector3 carDir          = transform.forward;
            float   fVelo           = rigidbody.velocity.magnitude;
            Vector3 veloDir         = rigidbody.velocity * (1 / fVelo);
            float   angle           = -Mathf.Asin(Mathf.Clamp(Vector3.Cross(veloDir, carDir).y, -1, 1));
            float   optimalSteering = angle / (CarWheels[0].maxSteeringAngle * Mathf.Deg2Rad);

            if (fVelo < 1)
            {
                optimalSteering = 0;
            }

            float steerInput = 0;

            steerInput = Input.GetAxisRaw(ControlsSteeringBinding);

            if (steerInput < 0)
            {
                steerInput = -1;
            }
            if (steerInput > 0)
            {
                steerInput = 1;
            }

            if (steerInput < steering)
            {
                float steerSpeed = (steering > 0) ? (1 / (steerReleaseTime + veloSteerReleaseTime * fVelo)) : (1 / (steerTime + veloSteerTime * fVelo));
                if (steering > optimalSteering)
                {
                    steerSpeed *= 1 + (steering - optimalSteering) * steerCorrectionFactor;
                }
                steering -= steerSpeed * Time.deltaTime;
                if (steerInput > steering)
                {
                    steering = steerInput;
                }
            }
            else if (steerInput > steering)
            {
                float steerSpeed = (steering < 0) ? (1 / (steerReleaseTime + veloSteerReleaseTime * fVelo)) : (1 / (steerTime + veloSteerTime * fVelo));
                if (steering < optimalSteering)
                {
                    steerSpeed *= 1 + (optimalSteering - steering) * steerCorrectionFactor;
                }
                steering += steerSpeed * Time.deltaTime;
                if (steerInput < steering)
                {
                    steering = steerInput;
                }
            }

            // Throttle/Brake



            bool ThrottlePressed = (Input.GetAxisRaw(ControlsThrottleBinding) > 0) ? true : false;
            bool BrakePressed    = (Input.GetAxisRaw(ControlsBrakeBinding) > 0) ? true : false;

            if (CarSetup.EngineData.Automatic && CarEngine.CurrentGear == 0)
            {
                ThrottlePressed = (Input.GetAxisRaw(ControlsBrakeBinding) > 0) ? true : false;
                BrakePressed    = (Input.GetAxisRaw(ControlsThrottleBinding) > 0) ? true : false;
            }

            if (Input.GetKey(KeyCode.LeftShift))
            {
                throttle      = 1;
                throttleInput = 1;
            }
            else if (ThrottlePressed)
            {
                if (throttleInput > 0)
                {
                    throttle += Time.deltaTime / throttleSpeed;
                }
                else if (!tractionControl)
                {
                    throttle = 1;
                }
                else
                {
                    throttle -= Time.deltaTime / throttleReleaseTime;
                }

                if (throttleInput < 0)
                {
                    throttleInput = 0;
                }
                throttleInput += Time.deltaTime / throttleSpeed;
                throttle       = 1;
            }
            else
            {
                if (CarEngine.slipRatio < 0.2f)
                {
                    throttle      -= Time.deltaTime / throttleReleaseTime;
                    throttleInput -= Time.deltaTime / throttleReleaseTimeTraction;
                }
                else
                {
                    throttle      -= Time.deltaTime / throttleReleaseTimeTraction;
                    throttleInput -= Time.deltaTime / throttleReleaseTimeTraction;
                }
            }

            throttle = Mathf.Clamp01(throttle);

            if (BrakePressed)
            {
                if (CarEngine.slipRatio < 0.2f)
                {
                    brake += Time.deltaTime / throttleSpeed;
                }
                else
                {
                    brake += Time.deltaTime / throttleTimeTraction;
                }
                throttleInput -= Time.deltaTime / throttleSpeed;
            }
            else
            {
                if (CarEngine.slipRatio < 0.2f)
                {
                    brake -= Time.deltaTime / throttleReleaseTime;
                }
                else
                {
                    brake -= Time.deltaTime / throttleReleaseTimeTraction;
                }
                brake = 0;
            }



            brake         = Mathf.Clamp01(brake);
            throttleInput = Mathf.Clamp(throttleInput, -1, 1);

            // Handbrake
            if (Input.GetAxisRaw(ControlsHandbrakeBinding) > 0)
            {
                handbrake = 1;
            }
            else
            {
                handbrake = 0;
            }

            // Gear shifting
            float shiftThrottleFactor = Mathf.Clamp01((Time.time - lastShiftTime) / shiftSpeed);


            if (RaceManager.IsRaceStarted && !RaceRegister.IsRacerFinished)
            {
                CarSetup.EngineData.Automatic = true;

                if (!IsFirstGearShifted)
                {
                    CarEngine.CurrentGear = 2;
                    IsFirstGearShifted    = true;
                }


                if (Input.GetButtonDown(ControlsShiftUpBinding))
                {
                    lastShiftTime = Time.time;
                    CarEngine.ShiftUp();
                }
                if (Input.GetButtonDown(ControlsShiftDownBinding))
                {
                    lastShiftTime = Time.time;
                    CarEngine.ShiftDown();
                }
            }
            else if (RaceRegister.IsRacerFinished)
            {
                steering  = 0;
                throttle  = 0f;
                handbrake = 0.5f;
                CarSetup.EngineData.Automatic = true;
                CarEngine.CurrentGear         = 1;
            }
            else
            {
                steering = 0;
                CarSetup.EngineData.Automatic = false;
                CarEngine.CurrentGear         = 1;
                handbrake = 1f;
            }

            CarEngine.throttle = throttle * throttleBonus;
            //CarEngine.throttleInput = throttleInput * throttleBonus;
            CarEngine.brake = brake;



            // Apply inputs

            CarEngine.brake     = brake;
            CarEngine.handbrake = handbrake;
            CarEngine.steer     = steering;

            CheckIsCarStuck();
            ProcessLights();
        }
Ejemplo n.º 2
0
        void Update()
        {
            // Steering
            Vector3 carDir          = transform.forward;
            float   fVelo           = rigidbody.velocity.magnitude;
            Vector3 veloDir         = rigidbody.velocity * (1 / fVelo);
            float   angle           = -Mathf.Asin(Mathf.Clamp(Vector3.Cross(veloDir, carDir).y, -1, 1));
            float   optimalSteering = angle / (CarWheels[0].maxSteeringAngle * Mathf.Deg2Rad);

            if (fVelo < 1)
            {
                optimalSteering = 0;
            }

            float steerInput = 0;

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                steerInput = -1;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                steerInput = 1;
            }

            if (steerInput < steering)
            {
                float steerSpeed = (steering > 0) ? (1 / (steerReleaseTime + veloSteerReleaseTime * fVelo)) : (1 / (steerTime + veloSteerTime * fVelo));
                if (steering > optimalSteering)
                {
                    steerSpeed *= 1 + (steering - optimalSteering) * steerCorrectionFactor;
                }
                steering -= steerSpeed * Time.deltaTime;
                if (steerInput > steering)
                {
                    steering = steerInput;
                }
            }
            else if (steerInput > steering)
            {
                float steerSpeed = (steering < 0) ? (1 / (steerReleaseTime + veloSteerReleaseTime * fVelo)) : (1 / (steerTime + veloSteerTime * fVelo));
                if (steering < optimalSteering)
                {
                    steerSpeed *= 1 + (optimalSteering - steering) * steerCorrectionFactor;
                }
                steering += steerSpeed * Time.deltaTime;
                if (steerInput < steering)
                {
                    steering = steerInput;
                }
            }

            // Throttle/Brake

            bool accelKey = Input.GetKey(KeyCode.UpArrow);
            bool brakeKey = Input.GetKey(KeyCode.DownArrow);

            if (CarSetup.EngineData.Automatic && CarEngine.CurrentGear == 0)
            {
                accelKey = Input.GetKey(KeyCode.DownArrow);
                brakeKey = Input.GetKey(KeyCode.UpArrow);
            }

            if (Input.GetKey(KeyCode.LeftShift))
            {
                throttle      += Time.deltaTime / throttleTime;
                throttleInput += Time.deltaTime / throttleTime;
            }
            else if (accelKey)
            {
                if (CarEngine.slipRatio < 0.10f)
                {
                    throttle += Time.deltaTime / throttleTime;
                }
                else if (!tractionControl)
                {
                    throttle += Time.deltaTime / throttleTimeTraction;
                }
                else
                {
                    //throttle -= Time.deltaTime / throttleReleaseTime;
                    throttle = 0;
                }

                if (throttleInput < 0)
                {
                    throttleInput = 0;
                }
                throttleInput += Time.deltaTime / throttleTime;
                brake          = 0;
            }
            else
            {
                if (CarEngine.slipRatio < 0.2f)
                {
                    throttle -= Time.deltaTime / throttleReleaseTime;
                }
                else
                {
                    throttle -= Time.deltaTime / throttleReleaseTimeTraction;
                }
            }

            throttle = Mathf.Clamp01(throttle);

            if (brakeKey)
            {
                if (CarEngine.slipRatio < 0.2f)
                {
                    brake += Time.deltaTime / throttleTime;
                }
                else
                {
                    brake += Time.deltaTime / throttleTimeTraction;
                }
                throttle       = 0;
                throttleInput -= Time.deltaTime / throttleTime;
            }
            else
            {
                if (CarEngine.slipRatio < 0.2f)
                {
                    brake -= Time.deltaTime / throttleReleaseTime;
                }
                else
                {
                    brake -= Time.deltaTime / throttleReleaseTimeTraction;
                }
            }



            brake         = Mathf.Clamp01(brake);
            throttleInput = Mathf.Clamp(throttleInput, -1, 1);

            // Handbrake
            if (Input.GetKey(KeyCode.Space))
            {
                handbrake = 1;
            }
            else
            {
                handbrake = 0;
            }

            // Gear shifting
            //float shiftThrottleFactor = Mathf.Clamp01((Time.time - lastShiftTime) / shiftSpeed);


            CarEngine.throttle = throttle;// *shiftThrottleFactor;
            // CarEngine.throttleInput = throttleInput;
            CarEngine.brake = brake;

            if (Input.GetKeyDown(KeyCode.A))
            {
                lastShiftTime = Time.time;
                CarEngine.ShiftUp();
            }
            if (Input.GetKeyDown(KeyCode.Z))
            {
                lastShiftTime = Time.time;
                CarEngine.ShiftDown();
            }



            // Apply inputs
            //foreach (RGKCar_Wheel w in CarWheels)
            //{
            //    w.brake = brake;
            //    w.handbrake = handbrake;
            //    w.steering = steering;
            //}
            CarEngine.brake     = brake;
            CarEngine.steer     = steering;
            CarEngine.handbrake = handbrake;

            ProcessLights();
        }