- Home /
Slowing game speed to reach target fps?
Hi, I'm new here in the forums, but have basic knowledge of Unity3D.
I'm trying to make the game always reach a certain fps (frames per second, not first person shooter) even if the player's pc is crappy. I'm not talking about VSync here, but slowing down the game to make sure it is smooth.
It's like running a game under it's requirements. It lags and possibly go slow-mo.
One example of what I'm trying to achieve is Hotline Miami. Try playing it with a beefy computer and you'll realize everything's slowed down (even bullets and movements...). Well, I'm considering to use Time.timeScale but I can't figure it out to match fps and all those things.
The reason I'm trying to do this is I need to repeat some player movements again and again but if the fps isn't constant, the repeating player movements matches the time exactly. Like during the 10th second, player will jump, instead of the 600th frame.
Thanks
Answer by Bip901 · Dec 11, 2017 at 10:48 AM
I strongly recommend multiplying your movements by Time.deltaTime instead of slowing the whole game down.
Time.deltaTime is the time [in seconds] passed since the last frame update. If you have a fast computer, this number will be very small. If you have a slow computer, this number will be higher. So instead of moving the player by X units, move it by X * Time.deltaTime units. This will make the player move in the same pace in every computer.
If I didn't understand your question well and you actually meant limiting the FPS, use this:
Application.targetFrameRate = 30;
Well, let's make this clearer. I'll split into 2 phases so it'd be much understandable.
Phase 1: The player moves around and I record the positions of the player every frame into an array. So if the player is playing at 60fps for 10 seconds, I get 600 frames, also 600 recorded positions.
Phase 2: I play out all movements in the exact ti$$anonymous$$g. 600 positions will play out exactly 10 seconds if the player has a constant 60fps.
The Problem: What if the player's computer slowed down during Phase 1? Example: The player plays the game for 10 seconds with 30fps (300 frames). And suddenly the player's computer sped up in Phase 2 and runs 60fps. If I play out the positions frame by frame, Phase 2 will only run 5 seconds in 60fps (300 frames). I need it to run fully 10 seconds, so probably I smooth out the positions of the player. But what if it's vice versa, the player got 60fps for 10 seconds and in phase 2 got 30fps? I know it's to half the positions (skip some frames). But if the player's fps isn't constant throughout the whole game?
Solution: $$anonymous$$aybe I can use Time.deltaTime. That's exactly what I'm planning: to run the player's movement through the physics engine. But there's another script recording the player's movement (How to manipulate and use Time.deltaTime I dont know). In Phase 2 the movement and positions $$anonymous$$UST match the time (or more or less a few millisecond slower/faster). It's required in my game's concept, so I hope to achieve this.
Almost forgot, thanks for the reply!
If you want it to depend on time, you have to use time. You can look at the Unity way of storing an AnimationClip, which is like playing a frame animation, but not so, they interpolate it at runtime.
Is it the positions interpolating or the animation (graphics) interpolating? I need the positions with all attached colliders and rigidbodies so the player can manipulate what he has previously done
Agreed, IF the time that elapses between each collected data point varies, then you need to store those time intervals along with the positions, and do some kind of interpolation on playback.
But couldn't you use FixedUpdate to store and playback, ins$$anonymous$$d of Update..?
I'm not even sure how FixedUpdate runs exactly. How many times it runs per second... But if it's time dependent, that means I have to collect user input in Update and collect positions in FixedUpdate?
Well, I'm actually doing the same thing in my game :D I have a little soccer game that records the game, and after the game ends,it shows the recorded replay. I use
void FixedUpdate()
{
recordedPositions.Add(transform.position);
}
And since I multiply all of the movements in the game with Time.deltaTime, it replays perfectly at any frame rate (when I play the replay, I do it on FixedUpdate as well).
Answer by TheSorrowRaven · Dec 14, 2017 at 12:12 PM
The answers are in the comments in the answer of Bip901.
Basically, use FixedUpdate() to collect data and replay data
@Bip901 @haruna9x @Bonfire-Boy
Thanks for all the help
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Sprites performance issue 0 Answers
[Android] Debug vs Release build FPS 0 Answers
fpscontrol how do i login on unity 1 Answer
How to calculate an average frame rate 3 Answers