I am trying to speed up and slow down time using time.timescale in unityscript.
what happens is the fast motion code never runs, and I cant set the time back to normal. Any help with the code?
public var slow : boolean = false;
public var fast : boolean = false;
function Update () {
if(Input.GetButtonUp("Slomo")){
if(!slow){
Time.timeScale = .5;
slow=true;
}else{
if(Input.GetButtonUp("Fast")){
if(!fast){
Time.timeScale = 2;
fast=true;
}else{
Time.timeScale = 1;
slow=false;
fast=false;
}
}
}
}
}
Your if statements are all over the place. This might be easier to read:
public var slow : boolean = false;
public var fast : boolean = false;
function Update () {
if(Input.GetButtonUp("Slomo")){
if(!slow){
Time.timeScale = .5;
slow=true;
}else{
if(Input.GetButtonUp("Fast")){
if(!fast){
Time.timeScale = 2;
fast=true;
}else{
Time.timeScale = 1;
slow=false;
fast=false;
}
}
}
}
}
So! Lets sort this out. What its currently doing is:
if you release the Slomo key, it sets it to slowmo if it was not already slowmo.
If it WAS slowmo and you just released the Fast key(which you probably never can do in the same fame) it will do the same for fastmode.
Do you want it to be a key you hold down for slowmo? Or 2 buttons to activate and deactivate, which is kind of a silly option. May as well be one button to turn it on and off.
Im gonna assume you want to hold down a button that makes it slowmo. Which is the simplest option.
Try this:
if(Input.GetButton("Slomo")){
slow = true;
Time.timeScale = 0.5;
} else {
slow = false;
Time.timeScale = 1;
}
You dont need the fast boolean if you have a slow one since they are always opposites.
What this code is doing essentially is; While you are holding down the Slomo key, it will be slow, else it will not be slow.
Hope this helps! You need a lot more practice!
Oh! And have you actually set up and Input button called “Slomo” in the editor? This will never work if you haven’t.