void Update()
        {
            if (textDebug == null)
            {
                return;
            }

            textDebug.text = "Tiny Car Controller Demo v1.4.0\n\nUse the arrow keys or WASD keys to control the car.\n\nPress C to toggle camera modes.\n\n";

            if (carController != null)
            {
                textDebug.text += "Speed : " + (int)carController.getForwardVelocity() + " m/s\n";
                textDebug.text += "Drift speed : " + (int)carController.getLateralVelocity() + " m/s\n";
                textDebug.text += "Is grounded : " + carController.isGrounded() + "\n";
                textDebug.text += "Ground type : " + carController.getSurfaceParameters().getName() + "\n";
                textDebug.text += "Is braking : " + carController.isBraking() + "\n";
            }

            if (carCamera != null)
            {
                if (Input.GetKeyDown(KeyCode.C))
                {
                    switch (carCamera.viewMode)
                    {
                    case TinyCarCamera.CAMERA_MODE.TopDown:
                        carCamera.viewMode = TinyCarCamera.CAMERA_MODE.ThirdPerson;
                        break;

                    case TinyCarCamera.CAMERA_MODE.ThirdPerson:
                        carCamera.viewMode = TinyCarCamera.CAMERA_MODE.TopDown;
                        break;
                    }
                }
            }
        }
Example #2
0
        void FixedUpdate()
        {
            float deltaTime = Time.fixedDeltaTime;

            // input

            if (enableInput)
            {
                float motorDelta    = getInput(forwardInput) - getInput(reverseInput);
                float steeringDelta = getInput(steerRightInput) - getInput(steerLeftInput);

                car.setSteering(steeringDelta);
                car.setMotor(motorDelta);
            }

            // visuals

            if (vehicleBody != null)
            {
                Vector3 smoothRotation = car.getGroundRotationSmooth().eulerAngles;
                if (!rotatePitch)
                {
                    smoothRotation.x = 0;
                }
                if (!rotateRoll)
                {
                    smoothRotation.z = 0;
                }

                vehicleBody.rotation = Quaternion.Euler(smoothRotation);

                vehicleBody.position = car.getBodyPosition();
            }

            wheelSpin    += car.getForwardVelocity() * deltaTime * wheelsSpinForce;
            wheelRotation = Mathf.Lerp(wheelRotation, car.getSteering() * wheelsMaxTurnAngle, wheelsTurnSmoothing <= 0 ? 1 : Mathf.Clamp01(wheelsTurnSmoothing * deltaTime));

            foreach (Transform t in wheelsFront)
            {
                t.transform.localRotation = Quaternion.Euler(wheelSpin, wheelRotation, 0);
            }
            foreach (Transform t in wheelsBack)
            {
                t.transform.localRotation = Quaternion.Euler(wheelSpin, 0, 0);
            }

            // particles

            // drifting smoke

            if (particlesDrifting != null)
            {
                if (Mathf.Abs(car.getLateralVelocity()) > minDriftingSpeed && car.isGrounded())
                {
                    if (!particlesDrifting.isPlaying)
                    {
                        particlesDrifting.Play();
                    }
                }
                else
                {
                    if (particlesDrifting.isPlaying)
                    {
                        particlesDrifting.Stop();
                    }
                }
            }

            // collision sparks

            if (particlesSideCollision != null)
            {
                if (car.hasHitSide(minSideCollisionForce))
                {
                    particlesSideCollision.transform.position = car.getSideHitPosition();
                    particlesSideCollision.Play();
                }
            }

            if (particlesLanding != null)
            {
                if (car.hasHitGround(minLandingForce))
                {
                    particlesLanding.Play();
                }
            }

            if (particlesSideFriction != null)
            {
                if (car.isHittingSide() && car.getGroundVelocity() > minSideFrictionVelocity)
                {
                    particlesSideFriction.transform.position = car.getSideHitPosition();
                    if (!particlesSideFriction.isPlaying)
                    {
                        particlesSideFriction.Play();
                    }
                }
                else
                {
                    if (particlesSideFriction.isPlaying)
                    {
                        particlesSideFriction.Stop();
                    }
                }
            }
        }