Esempio n. 1
0
    private void RunFlightModelRotations(float deltaTime)
    {
        PitchG         = Maths.CalculatePitchG(transform, Velocity, PitchRate);
        PitchGSmoothed = SmoothDamp.Move(PitchGSmoothed, PitchG, 3f, deltaTime);

        // The stall speed affects low speed handling. The lower the stall speed, the more control
        // the plane has at low speeds. A high stall speed results in not only poor control at low
        // speed, but also requires more speed to generate the maximum turn rate.
        var controlAuthority = GetControlAuthority();

        // Limit pitch input based on G. This is a reactive system. At low framerates (e.g. 10) the
        // sample rate will cause oscillations similar to RPMs bouncing off a rev limiter. A better
        // way to do this would be to pre-calculate a max turn rate based for a given G.
        float gLerp = PitchG > 0
            ? Mathf.InverseLerp(MaxG, MaxG + MaxG * .1f, PitchG)
            : Mathf.InverseLerp(-MinG, -MinG - MinG * .1f, PitchG);
        var gLimiter = Mathf.Lerp(0f, 1f, 1f - gLerp);

        // For each axis, generate a rotation and then damp it to create smooth motion.
        var targetPitch = FlightInput.Pitch * MaxPitchRate * gLimiter * controlAuthority;

        PitchRate = SmoothDamp.Move(PitchRate, targetPitch, PitchResponse, deltaTime);
        var pitchRotation = Quaternion.AngleAxis(PitchRate * deltaTime, Vector3.right);

        var targetYaw = FlightInput.Yaw * MaxYawRate * controlAuthority;

        YawRate = SmoothDamp.Move(YawRate, targetYaw, YawResponse, deltaTime);
        var yawRotation = Quaternion.AngleAxis(YawRate * deltaTime, Vector3.up);

        var targetRoll = FlightInput.Roll * MaxRollRate * controlAuthority;

        RollRate = SmoothDamp.Move(RollRate, targetRoll, RollResponse, deltaTime);
        var rollRotation = Quaternion.AngleAxis(-RollRate * deltaTime, Vector3.forward);

        transform.localRotation *= pitchRotation * rollRotation * yawRotation;

        // When stalling, the plane pitches down towards the ground.
        var stallRate = GetStallRate();

        if (stallRate > 0f)
        {
            // Generate stall rotation.
            var stallAxis = Vector3.Cross(transform.forward, Vector3.down);
            transform.rotation = Quaternion.AngleAxis(stallRate * deltaTime, stallAxis) * transform.rotation;
        }
    }