Beispiel #1
0
    void FixedUpdate()
    {
        float motorTorque = maxMotorTorque * Input.GetAxis("Vertical");
        float brakeTorque = maxBrakeTorque * Input.GetAxis("Jump");

        flController.ApplyThrottle(motorTorque);
        frController.ApplyThrottle(motorTorque);
        rlController.ApplyThrottle(motorTorque);
        rrController.ApplyThrottle(motorTorque);


        flController.ApplyBrake(brakeTorque);
        frController.ApplyBrake(brakeTorque);
        rlController.ApplyBrake(brakeTorque);
        rrController.ApplyBrake(brakeTorque);


        float steerAngle = steerRatio * Input.GetAxis("Horizontal");// Input.acceleration.x;//

        flController.ApplySteer(steerAngle);
        frController.ApplySteer(steerAngle);

        if (rb.velocity.magnitude > topSpeed)
        {
            float slowDownRatio = rb.velocity.magnitude / topSpeed;
            rb.velocity /= slowDownRatio;
        }
    }
Beispiel #2
0
    private void FixedUpdate()
    {
        Debug.Log("throttle = " + frontLeft.GetComponent <WheelCollider>().motorTorque + ", brake = " + frontLeft.GetComponent <WheelCollider>().brakeTorque);

        if (pathPointIdx >= pathPoints.Length)
        {
            flController.ApplyBrake(maxBrakeTorque);
            frController.ApplyBrake(maxBrakeTorque);
            rlController.ApplyBrake(maxBrakeTorque);
            rrController.ApplyBrake(maxBrakeTorque);
            return;
        }
        if (pathPointIdx < pathPoints.Length)
        {
            int bestIdx = pathPointIdx;

            Vector3 bestPoint = transform.InverseTransformPoint(new Vector3(
                                                                    pathPoints[pathPointIdx].position.x,
                                                                    transform.position.y,
                                                                    pathPoints[pathPointIdx].position.z
                                                                    ));
            for (int i = pathPointIdx + 1; i < pathPoints.Length && i < pathPointIdx + nextSteps; ++i)
            {
                Vector3 point = transform.InverseTransformPoint(new Vector3(
                                                                    pathPoints[i].position.x,
                                                                    transform.position.y,
                                                                    pathPoints[i].position.z
                                                                    ));
                if (point.z > 0)
                {
                    if ((point.x / point.z) < (bestPoint.x / bestPoint.z))
                    {
                        bestPoint = point;
                        bestIdx   = i;
                    }
                }
            }

            pathPointIdx = bestIdx;
            Debug.Log("go to point" + bestIdx);



            Vector3 steerVector = transform.InverseTransformPoint(new Vector3(
                                                                      pathPoints[pathPointIdx].position.x,
                                                                      transform.position.y,
                                                                      pathPoints[pathPointIdx].position.z
                                                                      ));

            //Debug.Log(steerVector.ToString());


            if (bestPoint.magnitude < distThreshold)
            {
                Debug.Log("passed point" + pathPointIdx + "[" + pathPoints[pathPointIdx].position.x + "," + pathPoints[pathPointIdx].position.y + "," + pathPoints[pathPointIdx].position.z + "]");

                ++pathPointIdx;
            }

            float newSteer = maxSteer * (steerVector.x / steerVector.magnitude);

            float newMotorTorque = maxMotorTorque;// * (1 - Mathf.Abs(steerVector.x / steerVector.magnitude));

            if (reversing)
            {
                newMotorTorque *= -1;
            }

            /*if (isReverse)
             * {
             *  Debug.Log("isReverse; go to point" + bestPoint.x + "," + bestPoint.z);
             *  flController.ApplySteer(-maxSteer * Mathf.Sign(newSteer));
             *  frController.ApplySteer(-maxSteer * Mathf.Sign(newSteer));
             *
             *  flController.ApplyThrottle(-maxMotorTorque * 0.5f);
             *  frController.ApplyThrottle(-maxMotorTorque * 0.5f);
             *  rlController.ApplyThrottle(-maxMotorTorque * 0.5f);
             *  rrController.ApplyThrottle(-maxMotorTorque * 0.5f);
             * }
             * else*/
            if (flag == 0)
            {
                flController.ApplySteer(newSteer);
                frController.ApplySteer(newSteer);
            }

            flController.ApplyThrottle(newMotorTorque);
            frController.ApplyThrottle(newMotorTorque);
            rlController.ApplyThrottle(newMotorTorque);
            rrController.ApplyThrottle(newMotorTorque);
        }
        if (rb.velocity.magnitude > topSpeed)
        {
            float slowDownRatio = rb.velocity.magnitude / topSpeed;
            rb.velocity /= slowDownRatio;
        }


        Sensor();
    }