Im trying to get a slow motion system working

I’ve been trying to make a slow motion system but it doesnt work
this is the code that i used:

bool ctrlDown = false;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftControl)) {
        ctrlDown = true;
} else {
        ctrlDown = false;
}
if (ctrlDown == true) {
    Time.timeScale = 0.1f;
} else {
    Time.timeScale = 1f;
    }
}

im trying to make it so when you press control it goes into slow motion

Hello!

GetKeyDown method returns true only in the first frame of pressing button.
If you keep button pressed, it returns false.

Try using GetKey instead.

Hope this helps.

You used GetKeyDown, but GetKeyDown only triggers an event once even if you hold it down, so you have to replace it with GetKey

Here are shortened versions of your code. If you want time to slow only while you are pressing control, use Input.GetKey() this:

 void Update()  {
     // Slow time while control is pressed.
     if (Input.GetKey(KeyCode.LeftControl))
         Time.timeScale = 0.1f;
     else
         Time.timeScale = 1f;
 }

If you wanted to toggle the slow motion with control, then use Input.GetKeyDown() like this:

 bool slowTime = false;

 void Update()  {
     // When CTRL is pressed, set "slowTime" to the opposite of it's current value.
     if (Input.GetKeyDown(KeyCode.LeftControl))
         slowTime = !slowTime;
     if (slowTime)
         Time.timeScale = 0.1f;
     else
         Time.timeScale = 1;

 }

The reason why your code wasn’t working before was because you were using Input.GetKeyDown(), which will only detect the key press for the frame that the key was pushed down.

Input.GetKey() detects continuously so time will slow while CTRL is pressed.


I hope I helped you understand what went wrong and how I fixed it!