2
\$\begingroup\$

I have a game (C++) in which I want to transition between two speeds of motion. For illustration, the player raises their weapon to aim, and whilst this is in progress over a known duration, time should slow down incrementally until it reaches the target rate.

Slowing it down linearly is easy. We have a start speed (e.g. 1.0), an end speed (e.g. 0.2), and can determine where we are in the fixed-length transition (e.g. 2000ms)

However if I slow it down linearly, the look and feel is just wrong. I suspect that to feel 'right', it should slow down quickly early in the transition, and then at a decreasing rate towards the end.

My maths is less than brilliant; it's been a long time since I graphed a function, and I rarely if ever introduce this kind of stuff into code. At a guess, using part of a 1/x or x^2 curve might give me what I want, but I'd still have to parametrise it to suit.

This or something like it must be a regularly-solved problem, so what's the simplest approach I can use here?

\$\endgroup\$
2
\$\begingroup\$

Here's a function that interpolates between Start and End quadratically by T.

float Qerp (float Start, float End, T)
{
    //The quadratic stuff
    T = 1 - T;
    T = 1 - (T * T);

    float Difference = End - Start;

    return (Start + (Difference * T));
}

I.e.:

float Elapsed = 0;
const float Length = 2.5f;
while (true)
{
    Time.timeScale = Qerp (1f,.2f,Elapsed / Length);

    Elapsed += Time.deltaTime;
    if (Elapsed > 1) break;
    yield return null;
}

This sets Time.timeScale from 1 to .2 over the course of 2.5 seconds. As the elapsed time of this process increases, the amount at which Time.timeScale changes decreases.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.