private void FixedUpdate()
    {
        if (jump && IsAtLeastHalfGrounded())
        {
            rb.AddRelativeForce(Vector3.up * jumpForceMult, ForceMode.VelocityChange);
            SoundController.instance.PlayVariation(jumpClip);
        }

        if (!IsAtLeastHalfGrounded())
        {
            rb.angularVelocity = -transform.forward * horizontalInput * rotationMult
                                 + transform.right * verticalInput * rotationMult;
        }

        foreach (AxleInfo axleInfo in axleInfos)
        {
            if (axleInfo.steering)
            {
                axleInfo.leftWheel.steerAngle  = horizontalInput * maxSteeringAngle;
                axleInfo.rightWheel.steerAngle = horizontalInput * maxSteeringAngle;
            }
            if (axleInfo.motor)
            {
                float lRpm = axleInfo.leftWheel.rpm;
                if (lRpm * verticalInput != 0 && Math.Sign(lRpm) != Math.Sign(verticalInput))
                {
                    axleInfo.leftWheel.brakeTorque = Mathf.Abs(verticalInput) * brakeTorque;
                }
                else
                {
                    axleInfo.leftWheel.brakeTorque = 0;
                    axleInfo.leftWheel.motorTorque = verticalInput * maxMotorTorque;
                }
                float rRpm = axleInfo.rightWheel.rpm;
                if (rRpm * verticalInput != 0 && Math.Sign(rRpm) != Math.Sign(verticalInput))
                {
                    axleInfo.rightWheel.brakeTorque = Mathf.Abs(verticalInput) * brakeTorque;
                }
                else
                {
                    axleInfo.rightWheel.brakeTorque = 0;
                    axleInfo.rightWheel.motorTorque = verticalInput * maxMotorTorque;
                }
            }
            WheelUtils.ApplyLocalPositionToVisuals(axleInfo.leftWheel);
            WheelUtils.ApplyLocalPositionToVisuals(axleInfo.rightWheel);
        }
    }
Beispiel #2
0
    private void FixedUpdate()
    {
        if (target == null)
        {
            return;
        }

        Vector3 targetDir = target.position - transform.position;
        float   diffAngle = Vector3.SignedAngle(transform.forward,
                                                targetDir,
                                                Vector3.up);
        float motor    = maxMotorTorque;
        float steering = axleInfos[0].leftWheel.steerAngle;

        float targetSteering;

        if (Mathf.Abs(diffAngle) <= steeringAngleMargin)
        {
            targetSteering = 0f;
        }
        else
        {
            targetSteering = Mathf.Clamp(diffAngle, -maxSteeringAngle, maxSteeringAngle);
        }
        steering = Mathf.MoveTowards(steering,
                                     targetSteering,
                                     steeringSens * Time.fixedDeltaTime);

        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;
            }
            WheelUtils.ApplyLocalPositionToVisuals(axleInfo.leftWheel);
            WheelUtils.ApplyLocalPositionToVisuals(axleInfo.rightWheel);
        }
    }