- Home /
Timer manipulation with timescale not working correctly
I am currently creating a game where you have a character moving like in the game "Lemmings" and there is a button to adjust the speed of the game.
I adjust this speed by changing the timescale and the character indeed moves,jumps, falls etc faster and the Timer included in the game also counts up faster.
But there is a problem in where I beat a certain level on 1.0f timescale in 23 seconds, but when I speed up the entire level to 2.0f its 18 seconds.
Shouldn't these both be 23 seconds as well? Since everything moves faster on the timescale?
I checked this with a real life stopwatch and the 23 seconds were correct, but the second level time(sped up) I would except it would be about 11.5 seconds(half of the 23 since it would be 2.0f), but its 9.5 seconds.
Now my question is how can I make it so even if I speed up time or slow down time the end time will always be the same(when going slow or fast with the speed)?
Adjusting the speed of just the character is out of the question since some obstacles etc don't work anymore then based on their movespeed/jumplength etc
Here is the code how my time shows up:
void Update()
{
minutes = (int)(Time.timeSinceLevelLoad / 60f);
seconds = (int)(Time.timeSinceLevelLoad % 60f);
timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
Here is the code my SpeedUp button uses
public void GameSpeed()
{
if (currentTimeSpeed == timeSpeedSlow)
{
Time.timeScale = timeSpeedFast;
currentTimeSpeed = timeSpeedFast;
}
else
{
Time.timeScale = timeSpeedSlow;
currentTimeSpeed = timeSpeedSlow;
}
}
Where timeSpeedSlow is 1.0f and timeSpeedFast is 2.0f;
Answer by OtunGroup · Sep 24, 2018 at 01:35 PM
Hi, messing with the Timescale isn't typically the best of ideas. It would better for you to create a variable, such as a float called speed; and then multiply all of your movement calculations with this variable when you want your game to speed up and slow down.
So for example, when you want a rigidbody to move, you would do:
public float speed;
private void Update()
{
GetComponent<Rigidbody>().AddForce(Vector3.forward * speed);
}
And then, by editing your speed variable, you can change your objects to move faster or slower. If everything that moves shares the same variable, then they would all move at the same speed.
Hope this helps, -Greg
Thanks for the response Greg,
The problem with this is that my player character walks automatically, so it moves jumps etc also automatically. There are objects like a stairway that the character automatically jumps on every stairstep to get up(intended) If I would mess around with the force thats added on the rigidbody the way the stairs would probably still work, but it would start to look really weird
Also the time to complete a level would still be faster with it turned on, ins$$anonymous$$d of being the same without any change of speed between turned on/off
Thats the reason why I tried it with timescale so everything just moves faster and everything still works properly, just not the timer
Answer by UnityCoach · Sep 24, 2018 at 01:43 PM
@OtunGroup is right. Although, using Time.timeScale makes it easy to do things like Slow Motion, it will only apply to :
Physics controlled objects
Objects you control with Framerate independent transforms, using Time.deltaTime as a multiplier.
Ie :
[SerializeField] float _speed = 1f;
void Update ()
{
transform.Translate (Input.GetAxis("Horizontal") * _speed * Time.deltaTime, 0, 0);
}
Your answer
Follow this Question
Related Questions
Unity 5 - Adding a Pre-made Countdown Timer C# Script to the Game Scene as a Text UI Element 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Debug.Log() not working? 0 Answers
What is Matrix4x4? 2 Answers