Do Slowmotion effect for x seconds
Hello! I am currently working on my first 2d game and everytime when the player dies i want a slowmotion to appear for a few seconds. I didnt figure out how i can have a function for a certain amount of time. Can someone help me with the script? The trigger of the slowmo will be an if method
public float slowdownfactor;
public float slowdownlength;
public gameobject player;
public void start()
{
if (player.activeSelf = false)
{
Time.timeScale = slowdownfactor;
}
}
...and so on. Now i just need help so set the slowdown length. I hope someone can help me:) ~dvxl
Answer by The_Three_Vs · Mar 30, 2020 at 10:39 PM
You can use a coroutine:
public void start()
{
if(player.activeSelf == false)
{
StartCoroutine(WaitThenRestoreTime();
}
}
private IEnumerator WaitThenRestoreTime()
{
Time.timeScale = slowdownFactor;
yield return new WaitForSecondsRealtime(slowdownTime);
Time.timeScale = 1f;
}
This calls the WaitThenRestoreTime coroutine function with StartCoroutine. The coroutine will then activate the slowdown effect, wait a few seconds in real-time (as to not be affected by the reduced timeScale), and then return timeScale to normal. This is possible because coroutines run simultaneously to other methods, and can be "paused" in place using methods like WaitForSeconds. For a more in-depth explanation of coroutines, check out https://docs.unity3d.com/Manual/Coroutines.html
Oh, and just a quick reminder to use "==" instead of "=" when comparing values.
I hope this was helpful!
Follow this Question
Related Questions
PowerUp Timer 0 Answers
My code is not working - HELP - IEnumerator Coroutines 0 Answers
Why is my coroutine not working? 1 Answer
Power up not working 0 Answers
Could not load source 'Coroutines.cs': No source available. 1 Answer