I’m working on an action sidescroller(works on a 2D plane, 3D graphics) where the main mechanic is that you can slow down time, i.e. Player stays at the same speed, everything else slows down.
Problem is, when I change Time.timeScale everything slows down.
if (Input.GetKey(KeyCode.Space)) {
Time.timeScale = 1 / timeSlowMultiplyer; //timeSlowMultiplyer is 4
} else {
Time.timeScale = 1;
}
To counteract this I tried to change the player’s Time.deltaTime to undo the effects of the time scale
Noting: this assumes that your normal timeScale is 1.0. How it works:
essentially, what you want is to slow down time, but have the player move at normal speed… This can be represented with this small formula:
What this does? since the velocity is multiplied by 0.4 by timeScale, adding deltaTime + 1.0-timeScale effectively gives you a 1.6 multiplier to correct that for ONLY the player. (This would work for everything else too. ;))
I do get what you mean by the player jumping really high and superfast. Yes, timeScale is Time.timeScale in unity, i simply used the word for the pseudo-code example I gave.
The differences I have made are these:
Essentially, our previous formula:
fullVelocity = actualVelocity * ( deltaTime + inverse of timeScale )
Should actually be this:
full velocity = actual velocity * (time delta * (1 + (1-timeScale) ) )
What it does?
First… when Time.timeScale is 1.0, everyhting is normal. what we want is to keep the player at 1.0, but slow down everything else.
To do it, we need to multiply the slowed-down velocity by 1 + (1-time scale)
Example: say you have slowed down time so timeScale reads 0.4. what we do is add the inverse of that to 1 to get our number (in this case, the 1.6 multiplier mentioned earlier in my previous posts) The code above does exactly that.
Now when slowmo is active the player is moving faster but still slower than what normal speed speed should be, and also jumping higher than it should be but not as high as my original method.
See if this works! I might as well try slow-mo myself, not only to help you, but just for fun… That’s for another time though, maybe tomorrow I can get a simple slow-mo example working.
Wait hang on, are you saying the player’s slower but somehow, your jump height’s higher? That doesn’t sound right…
PROBLEMS SOLVED! BIG THING I DIDN’T REALIZE! my gravity “velocity.y += gravity * Time.deltaTime” was affected by deltaTime and i didn’t apply the corrections to it which is why the player kept jumping so high.
Also:
I have changed my approach and made everything have a different time scale.
I have a public class to store the variables
public class TimeScale {
//default timescales
public static float player = 1;
public static float enemies = 1;
public static float global = 1;
}