Need help in making slowmotion

i need help in making a script like when a player presses a key for example “z” the Slow motion starts for a limited time say for 5 seconds. And then again after 10 seconds we can use Slow motion. Any help will be appreciated. Thanks

Hello @nikhil_mane. I will assume that you want to put the whole game in slow motion (since that is the common thing in most games), and you can achieve that by simply using Time.timeScale and WaitForSeconds() in a coroutine function. The code would look something like this:

private bool enableSlowMo = true;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Z) && enableSlowMo == true)
    {
        StartCoroutine(SlowMotion());
    }
}

IEnumerator SlowMotion()
{
    Time.timeScale = 0.5f;
    enableSlowMo = false;

    yield return new WaitForSeconds(2.5f);

    Time.timeScale = 1.0f;

    yield return new WaitForSeconds(5.0f);

    enableSlowMo = true;
}

You can modify the value of Time.timeScale according to your liking. You can also add some zoom-in and zoom-out animations to make it look nicer.

Hope this helps :slight_smile:

Just set Time.timeScale to a value lower than 1.
For example set it to 0.5f to have 50% speed.
You could achieve the 5 seconds period by simply implementing a timer and also use another bool to store wheter you can start slow Motion again. Something like this:

public float slowMotionTimeScale = .5f;
public float slowMotionDuration = 5f;
public float slowMotionAvailableAgainDuration = 10f;
public KeyCode slowMotionKey = KeyCode.Z;

bool isInSlowMotion;
float timer;

void Update()
{
    if (!isInSlowMotion)
    {
        if (timer < slowMotionAvailableAgain)
        {
            timer += Time.deltaTime;
        }
        else
        {
            if (Input.GetKeyDown(slowMotionKey))
            {
                Time.timeScale = slowMotionTimeScale;
                timer = 0;
                isInSlowMotion = true;
            }
        }
    }
    else
    {
        if (timer < slowMotionDuration)
        {
            timer += Time.deltaTime;
        }
        else
        {
            Time.timeScale = 1;
            timer = 0;
            isInSlowMotion = false;
        }
    }
}