Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (cpManager == null)
        {
            cpManager = m_drive.rb.gameObject.GetComponent <CheckpointManager>();
        }

        if (cpManager.lap >= RaceMonitor.totalLaps + 1)
        {
            m_drive.highAccelSound.mute = true;
            m_drive.skidSound.mute      = true;
            m_drive.CarMove(0, finishSteer, 0.5f);
            return;
        }

        float vertical   = Input.GetAxis("Vertical");   //Keyboard Input
        float horizontal = Input.GetAxis("Horizontal"); //Keyboard Input
        float brake      = Input.GetAxis("Jump");       //Keyboard Input

        if (m_drive.rb.velocity.magnitude > 1 || !RaceMonitor.racing)
        {
            lastTimeMoving = Time.time;
        }

        /*RaycastHit hit;
         * if(Physics.Raycast(m_drive.rb.gameObject.transform.position,-Vector3.up,out hit,10))
         * {
         *  if(hit.collider.gameObject.tag == "Road")
         *  {
         *      lastPosition = m_drive.rb.gameObject.transform.position;
         *      lastRotation = m_drive.rb.gameObject.transform.rotation;
         *  }
         * }*/

        if (Time.time > (lastTimeMoving + 4) || m_drive.rb.gameObject.transform.position.y < -5)
        {
            m_drive.rb.gameObject.transform.position = cpManager.lastCP.transform.position + Vector3.up * 2;
            m_drive.rb.gameObject.transform.rotation = cpManager.lastCP.transform.rotation;

            //m_drive.rb.gameObject.transform.position = lastPosition;
            //m_drive.rb.gameObject.transform.rotation = lastRotation;
            m_drive.rb.gameObject.layer         = 8;
            this.GetComponent <Ghost>().enabled = true;
            Invoke("ResetLayer", 3);
        }

        if (!RaceMonitor.racing)
        {
            vertical = 0;
        }

        m_drive.CarMove(vertical, horizontal, brake);
        m_drive.CheckForSkid();
        m_drive.CalculateEngineSound();
    }
Esempio n. 2
0
    void FollowTracker()
    {
        if (!RaceMonitor.racing)
        {
            lastTimeMoving = Time.time;
            return;
        }

        if (cpManager == null)
        {
            cpManager = ai_drive.rb.gameObject.GetComponent <CheckpointManager>();
        }

        if (cpManager.lap >= RaceMonitor.totalLaps + 1)
        {
            ai_drive.highAccelSound.mute = true;
            ai_drive.skidSound.mute      = true;
            ai_drive.CarMove(0, finishSteer, 0.5f);
            return;
        }

        ProgressTracker();

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

        if (Time.time > (lastTimeMoving + 4) || ai_drive.rb.gameObject.transform.position.y < -5)
        {
            ai_drive.rb.gameObject.transform.position = cpManager.lastCP.transform.position + Vector3.up * 2;
            ai_drive.rb.gameObject.transform.rotation = cpManager.lastCP.transform.rotation;

            //ai_drive.rb.gameObject.transform.position =
            //    circuit.waypoints[currentTrackerWP].transform.position + Vector3.up * 3 +
            //    new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));
            tracker.transform.position          = ai_drive.rb.gameObject.transform.position;
            ai_drive.rb.gameObject.layer        = 8;
            this.GetComponent <Ghost>().enabled = true;
            Invoke("ResetLayer", 3);
        }

        Vector3 localTarget;

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

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

        float steer = Mathf.Clamp(targetAngle * steeringSensitivity, -1, 1) * Mathf.Rad2Deg;

        float speedFactor = ai_drive.currentSpeed / ai_drive.maxSpeed;

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

        float accel = 1;

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

        float brake = 0;

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

        float prevTorque = ai_drive.torque;

        if (speedFactor < 0.3f && ai_drive.rb.gameObject.transform.forward.y > 0.1f)
        {
            ai_drive.torque *= 3.0f;
            accel            = 1;
            brake            = 0;
        }

        ai_drive.CarMove(accel, steer, brake);
        ai_drive.torque = prevTorque;
    }