- Home /
Calcule Player's Speed based on Time.timeScale
Hey! I am trying to give my players some cool abilities, and getting some inspiration from DBS (DBS fans will understand) and Kameo - EoP I've decided to make super speed. There's only a problem with it.
The Problem
I'd like to use Time.timeScale to make some good slow-mo, to give the sensation of ultra high speed. But the problem is: it slows the player down as well.
Obviously, you might be thinking. Yes, I knew it would happen, so I've started looking for solutions. Keep in mind that what I want is to have the player at a scaled speed (just like if it wasn't affected by timeScale). I've tried the following ones:
speed * (Time.deltaTime + (1 + (1 - Time.timeScale)));
speed * Time.fixedUnscaledDeltaTime;
speed * Time.unscaledDeltaTime;
None of those work. I can't simply set the speed when the player is on slow mo, because the player will make the slow mo more intense within time, raising its speed (making the game slower).
So, I need a system that keeps the player at normal speed even if Time.timeScale is at 0.0001. Quite crazy, right? Another thing to keep in mind is this line:
Time.fixedDeltaTime = 0.02f * Time.timeScale;
I am making the Fixed Time Step scale within timeScale, so I can be sure that I have fluid moves. Help? Thanks in advance.
Answer by unit_nick · Oct 18, 2017 at 03:13 PM
Just to clarify. You use Time.fixedDeltaTime inside of FixedUpdate, it represents the time past since FixedUpdate was last called. You use Time.deltaTime inside of Update, it represents the time past since Update was last called.
To accelerate you speed you have to increase the value. Because Time.timeScale is less than 1 when slowed you must divide speed rather than multipy it. So..
speed /= Time.timeScale;
should work
It kinda of works. But just to let you know, whenever I set the game with a speed like, 0.05, it doesn't works. And that's because I'm still multiplying it by fixedDeltaTime (I use physics) to be sure that the movements don't happen depending of the framerate.
Also, Time.deltaTime is automatically converted to Time.fixedDeltaTime inside FixedUpdate, so it doesn't matter which one I use. It'll always give me the same result.
What happens is, when I multiply the player's speed by the fixed fixedDeltaTime (0.2 * Time.timeScale) I'm always decreasing the hell out of the player, and I needed a solution for this as well.
So what I did? I did this:
speed * (Time.deltaTime + (1 + (1 - Time.timeScale)));
Which is basically
speed * (1 - Time.timeScale + 1 + Time.deltaTime) =
speed * (1 - 0.5 + 1 + Time.deltaTime) =
speed * (1.5 + Time.deltaTime)
Do you get it? I'm not multiplying anything by below one numbers. I'm multiplying by numbers above one, increasing player's speed (and after that probably decreasing it with an always smaller fixedDeltaTime).
Thanks for taking your time to help me, but yours solution didn't work. :)
Also, Time.deltaTime is automatically converted to Time.fixedDeltaTime inside FixedUpdate, so it doesn't matter which one I use. It'll always give me the same result.
ummm.... really? where do you get this information?
and why go through all you complicated plus and $$anonymous$$us stuff when the correct solution is to divide by the time scale?
to add.... your solution of adding 0.5 will only work when timeScale is 0.5. and then you go and add Time.deltaTime which is a totally random value based on framerate. therefor they do not relate to each other in any way and will almost always produce inconsistent speeds, as i believe you are experiencing.
ummm.... really? where do you get this information?
I've got this information on Unity's Scripting $$anonymous$$anual.
and why go through all you complicated plus and $$anonymous$$us stuff when the correct solution is to divide by the time scale?
To not use Time.fixedDeltaTime or Time.timeScale as crude factors, because it doesn't work. There I set the negative of Time.timeScale + 1 + Time.deltaTime (or fixed, if you want). Logically this should work, but it doesn't.
your solution of adding 0.5 will only work when timeScale is 0.5.
Not really. It was an example. It works if the timeScale is above or equals to 0.1.
you go and add Time.deltaTime which is a totally random value based on framerate
As I said, it's not random. It's converted. Equals to fixedDeltaTime.
will almost always produce inconsistent speeds, as i believe you are experiencing
Nope. That's not happening. What is happening is that the speed gets reduced as timeScale gets reduced; therefor it is being affected by timeScale.
Any other ideas?
Just a suggestion. You could literally speed everything else down, like having a global speed multiplier for everything that is not the player (or maybe a couple of events) and when the time comes, just change that value to manually decrease the speed, ins$$anonymous$$d of using timeScale. The thing with timescale is that it affect physics and when you set it to some "extreme values" physics in your game will get funky, this isn't a problem if you aren't using the physics system extensively in your game but if you do, you are kind of shooting yourself in the foot.
I have tried already that, it doesn't give the same effects that I want.
Your answer
Follow this Question
Related Questions
Slow motion all but one - yet another round, hopefully the last 6 Answers
how to get smooth slow motion? 4 Answers
Individual slow motion rigidbodys 1 Answer
Rewind time (or problems with arrays) 1 Answer
Animation Problems 1 Answer