- Home /
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
Answer by shellash · Jan 21, 2017 at 09:54 AM
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.
Answer by Firechicken · Jan 21, 2017 at 02:37 PM
You used GetKeyDown, but GetKeyDown only triggers an event once even if you hold it down, so you have to replace it with GetKey
Answer by Loui_Studios · Jan 21, 2017 at 03:46 PM
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!
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How should I control a 'rake' that pushes objects around a table? 2 Answers
Player Control Script Makes Movement Buggy? 1 Answer
The best way to control speed of the animation together with audio sound that both must sync 0 Answers
Making a hand move with the mouse 1 Answer