Exemple #1
0
    public void FixedUpdate()
    {
        float motor    = maxMotorTorque * Input.GetAxis("Vertical");
        float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
        float brake    = maxBrakeTorque * 0;

        var velocity = GetComponent <Rigidbody>().velocity;
        var localVel = transform.InverseTransformDirection(velocity);

        foreach (AxleInfo axleInfo in axleInfos)
        {
            if (axleInfo.steering)
            {
                axleInfo.leftWheel.steerAngle  = steering;
                axleInfo.rightWheel.steerAngle = steering;
            }
            if (axleInfo.motor)
            {
                axleInfo.leftWheel.motorTorque  = motor;
                axleInfo.rightWheel.motorTorque = motor;
                if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("Vertical") < 0 && !isBraking)
                {
                    msp.AdjustVolume(1f);
                }
                else if (Input.GetAxis("Vertical") == 0)
                {
                    msp.AdjustVolume(0.1f);
                }
            }
            if (axleInfo.braking)
            {
                if (localVel.z > 0 && Input.GetAxis("Vertical") < 0 ||
                    localVel.z < 0 && Input.GetAxis("Vertical") > 0)
                {
                    brake = maxBrakeTorque * Mathf.Abs(Input.GetAxis("Vertical"));
                    if (localVel.z > 0)
                    {
                        isBraking = true;
                        BrakingLights(isBraking);
                    }
                }
                else if (localVel.z < 0 && Input.GetAxis("Vertical") < 0)  //(isBraking || isReversing)
                {
                    isBraking = false;
                    BrakingLights(isBraking);
                    isReversing = true;
                    ReverseLights(isReversing);
                }
                else
                {
                    isBraking = false;
                    BrakingLights(isBraking);
                    isReversing = false;
                    ReverseLights(isReversing);
                }
                axleInfo.leftWheel.brakeTorque  = brake;
                axleInfo.rightWheel.brakeTorque = brake;
            }
            ApplyLocalPositionToVisuals(axleInfo.leftWheel);
            ApplyLocalPositionToVisuals(axleInfo.rightWheel);
        }
    }