Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (cpm == null)
        {
            cpm = ds.rb.GetComponent <CheckpointManager>();
        }

        if (cpm.lap == RaceMonitor.totalLaps + 1)
        {
            ds.acceleration.Stop();
            ds.Go(0, finishSteer, 0);
            return;
        }

        float a     = Input.GetAxis("Vertical");
        float steer = Input.GetAxis("Horizontal");
        float brake = Input.GetAxis("Jump");

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

        RaycastHit hit;

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

        if (Time.time > lastTimeMoving + 4 || ds.rb.gameObject.transform.position.y < -5)
        {
            ds.rb.gameObject.transform.position = cpm.lastCP.transform.position + Vector3.up * 2;;
            ds.rb.gameObject.transform.rotation = cpm.lastCP.transform.rotation;
            ds.rb.gameObject.layer         = 9;
            GetComponent <Ghost>().enabled = true;
            Invoke("ResetLayer", 3);
        }

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

        ds.Go(a, steer, brake);

        ds.CheckSkidding();
        ds.CalculateEngineSound();
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (!RaceMonitor.racing)
        {
            lastTimeMoving = Time.time;
            return;
        }

        if (cpm == null)
        {
            cpm = ds.rb.GetComponent <CheckpointManager>();
        }

        if (cpm.lap == RaceMonitor.totalLaps + 1)
        {
            ds.acceleration.Stop();
            ds.Go(0, finishSteer, 0);
            return;
        }

        ProgressTracker();

        Vector3 localTarget;
        float   targetAngle;

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

        if (Time.time > lastTimeMoving + 3 || ds.rb.gameObject.transform.position.y < -5)
        {
            ds.rb.gameObject.transform.position = cpm.lastCP.transform.position + Vector3.up * 2;
            ds.rb.gameObject.transform.rotation = cpm.lastCP.transform.rotation;
            //    circuit.waypoints[currentTrackerWP].transform.position + Vector3.up * 2 + new Vector3(Random.Range(-1,1), 0, Random.Range(-1, 1));
            tracker.transform.position = cpm.lastCP.transform.position;
            //circuit.waypoints[currentTrackerWP].transform.position;
            ds.rb.gameObject.layer         = 9;
            GetComponent <Ghost>().enabled = true;
            Invoke("ResetLayer", 3);
        }

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

        targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
        float steer = Mathf.Clamp(targetAngle * steeringSensitivity, -1, 1) * Mathf.Sign(ds.currentSpeed);

        //float distFactor = distToTarget / totalDistance;
        float speedFactor  = ds.currentSpeed / ds.maxSpeed;
        float corner       = Mathf.Clamp(Mathf.Abs(targetAngle), 0, 90);
        float cornerFactor = corner / 90;

        float brake = 0;

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

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

        float prevTorque = ds.torque;

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

        ds.Go(acc, steer, brake);
        ds.CalculateEngineSound();
        ds.CheckSkidding();

        ds.torque = prevTorque;
    }