void Update()
    {
        if (checkpointManager == null)
        {
            checkpointManager = kc.rb.GetComponent <CheckpointManager>();
        }


        if (checkpointManager.lap == RaceStarter.totalLaps + 1)
        {
            kc.highAccSound.Stop();
            kc.Go(0, finishSteer, 0);
            return;
        }

        if (controlMode == ControlMode.Keyboard)
        {
            horizontal = Input.GetAxis("Horizontal");
            vertical   = Input.GetAxis("Vertical");
            brake      = Input.GetAxis("Jump");
        }

        if (kc.rb.velocity.magnitude > 1f || !RaceStarter.raceStart)
        {
            lastTimeMoving = Time.time;
        }

        RaycastHit hit;

        if (Physics.Raycast(kc.rb.gameObject.transform.position, -Vector3.up, out hit, 10))
        {
            if (hit.collider.gameObject.tag == "Road")
            {
                lastPos = kc.rb.gameObject.transform.position;
                lastRot = kc.rb.gameObject.transform.rotation;
            }
        }

        if (Time.time > lastTimeMoving + 4)
        {
            kc.rb.gameObject.transform.position = checkpointManager.lastCP.transform.position + Vector3.up * 2;
            kc.rb.gameObject.transform.rotation = checkpointManager.lastCP.transform.rotation;
            kc.rb.gameObject.layer = 8;
            this.GetComponent <Ghost>().enabled = true;
            Invoke("ResetLayer", 5);
        }



        if (!RaceStarter.raceStart)
        {
            vertical = 0;
        }
        if (isMine)
        {
            kc.Go(vertical, horizontal, brake);
            kc.CheckForSkid();
            kc.CalculateEngineSound();
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!RaceStarter.raceStart)
        {
            lastTimeMoving = Time.time;
            return;
        }

        if (checkpointManager == null)
        {
            checkpointManager = kc.rb.GetComponent <CheckpointManager>();
        }

        if (checkpointManager.lap == RaceStarter.totalLaps + 1)
        {
            kc.highAccSound.Stop();
            kc.Go(0, finishSteer, 0);
            return;
        }


        ProgressTracker();
        Vector3 localTarget;
        float   targetAngle;


        if (kc.rb.velocity.magnitude > 1)
        {
            lastTimeMoving = Time.time;
        }

        if (Time.time > lastTimeMoving + 4)
        {
            kc.rb.gameObject.transform.position = checkpointManager.lastCP.transform.position + Vector3.up * 2;
            kc.rb.gameObject.transform.rotation = checkpointManager.lastCP.transform.rotation;

            tracker.transform.position          = checkpointManager.lastCP.transform.position;
            kc.rb.gameObject.layer              = 8;
            this.GetComponent <Ghost>().enabled = true;
            Invoke("ResetLayer", 3);
        }

        if (Time.time < kc.rb.GetComponent <AvoidDetector>().avoidTime)
        {
            localTarget = tracker.transform.right * kc.rb.GetComponent <AvoidDetector>().avoidPath;
        }
        else
        {
            localTarget = kc.rb.gameObject.transform.InverseTransformPoint(tracker.transform.position);
        }

        targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

        float steer = Mathf.Clamp(targetAngle * steeringSens, -1, 1) * Mathf.Sign(kc.currentSpeed);

        float speedFactor = kc.currentSpeed / kc.maxSpeed;

        float corner       = Mathf.Clamp(Mathf.Abs(targetAngle), 0, 90);
        float cornerFactor = corner / 90f;

        float brake = 0;

        if (corner > 10 && speedFactor > 0.1f)
        {
            brake = Mathf.Lerp(0, 1 + speedFactor * brakeSens, cornerFactor);
        }

        float accel = 1f;

        if (corner > 20 && speedFactor > 0.2f)
        {
            accel = Mathf.Lerp(0, 1 * accelSens, 1 - cornerFactor);
        }



        kc.Go(accel, steer, brake);

        kc.CheckForSkid();
        kc.CalculateEngineSound();
    }