DEV Community

Cover image for Mathf.Exp()
Giorgi Eliozashvili
Giorgi Eliozashvili

Posted on

Mathf.Exp()

I am making a rail game where you pilot a cosmic ship and shoot down enemies, and I wanted to share what I used for smooth rotation.

I want to tell how Mathf.Exp() works in Unity. To make my ship tilt left or right smoothly, there are many options, but I stopped at Mathf.Exp().

First of all, what is Mathf.Exp()?

It is a mathematical exponent, an Euler number (e), which ≈ 2.713. The Mathf.Exp() method takes a power in parentheses and raises Euler’s constant to that same power.

private void ProcessRotation()
    {
        var pitch = -controlRotationFactor * _movement.y;
        var roll = -controlRotationFactor * _movement.x;

        var targetRotation = Quaternion.Euler(pitch, 0f, roll);

        transform.localRotation = Quaternion.Lerp
        (
            transform.localRotation,
            targetRotation,
            1f - Mathf.Exp(-rotationSpeed * Time.deltaTime)
        );
    }
Enter fullscreen mode Exit fullscreen mode

This is the code that calculates tilts. Let’s dive into it a little.

For example, we have a powerful PC which generates high FPS, because of that Time.deltaTime will be a very small number. Let’s use some numbers:

  • rotation speed = 20
  • Time.deltaTime = 0.02 (roughly 50 FPS)

Mathf.Exp(-20 * 0.02) = -0.4

This means Euler’s number (e) is raised to a negative exponent. Raising e to a negative power produces a small value. In this case, Mathf.Exp(-0.4) returns a small number, which is then subtracted from 1f.

Now, how does this work in Update method:


Before I tell you this, a few words about Lerp method. Since we use Quaternion.Lerp, I will talk about that one exactly. So Lerp receives 3 arguments: start unit, end unit and interpolation ratio. Imagine this interpolation ratio as a loading bar where 0 is literally 0%, and 1 is 100%

a - Start unit quaternion value, returned when t = 0
b - End unit quaternion value, returned when t = 1
t - Interpolation ratio. The value is clamped to the range [0, 1]


In a first frame, when you press button to tilt your ship, for example, to the left, Mathf.Exp returns a value (0.67 in our case) which is subtracted from 1f, and we get 0.33 as a result. This means that in one frame, the ship is tilted by 33% already, because first result is the highest. On the next frame, we take the same 33% but from the remainder, here is a breakdown:

1f
1f — 0.33 = 0.67
Now we take 33% of 0.67, which is 0.22
0.67 — 0.22 = 0.45
etc

With every new frame we receive a smaller number, and our eye sees this like this: ship is tilted on the left and slows down the tilt speed when it reaches the target angle. In math, this is an infinite process, but in Unity numbers are eventually rounded to almost 0.

Using 1fMathf.Exp(-speedRotation * Time.deltaTime) inside Quaternion.Lerp, we get the ideal wing pitch. The higher the player’s FPS, the smaller the steps, but the resulting smoothness and turn time will be the same whether on a low-end laptop or a powerful gaming PC.

Top comments (0)