- Home /
Game slows down after restart
Hello guys, i got a runner game and my player dies when it collides with an obstacle.I made a slow motion after the collision to make the game cooler. On the game over screen i got restart button and after i press it game starts again but the time is still slow. I couldn't solve the issue please help me. There is my slow-motion code
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public float slowdownFactor = 0.05f;
public float slowdownLength = 4f;
private void Update()
{
Time.timeScale += (1f / slowdownLength) * Time.unscaledDeltaTime;
Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f);
}
public void DoSlowMotion()
{
Time.timeScale = slowdownFactor;
Time.fixedDeltaTime = Time.timeScale * .02f;
}
}
and there is my Restart code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Restart : MonoBehaviour
{
public void RestartGame()
{
SceneManager.LoadScene("Level01");
}
}
and finally the collision code
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
audioSource.pitch = speed;
timeManager.DoSlowMotion();
FindObjectOfType<GameManager>().gameOver();
FindObjectOfType<GameManager>().EndGame();
CountTime();
}
}
Answer by Magso · Aug 05, 2019 at 11:49 PM
Reset Time.timeScale
to 1 when you reload the scene.
How did you manage to code so much without understanding this?
Thankss. I knew it was about Time.timeScale
but i couldn't manage to write to code for that. I tried Time.timeScale = 1f;
in RestartGame() but it didn't work. It is my first time coding in c# and unity so i can't find simple solutions sometimes.
If you wrote
public void RestartGame()
{
Scene$$anonymous$$anager.LoadScene("Level01");
Time.timeScale = 1f;
}
It won't work because you're reloading the level before resetting the timescale, you would have to swap the lines round.
Your answer
Follow this Question
Related Questions
Restart UI-Button not working 1 Answer
Time.timeScale not working 1 Answer
multiple GUI.Buttons at the same time 1 Answer
Measuring time? 2 Answers
How do I make a button not interactable for a set amount of time after pressed? 0 Answers