- Home /
How do you find Unity's frames/second?
I wanted to use Unity to create a small demo for a game I'd been designing, simply to test out the physics, but all of the physics-related writing I have done (movement speed, jumping height, etc.) are done in frames/second. I don't know Unity's framerate. I'm used to 2D games where you can set it. I'd planned on having the FPS set to 30, so all of my "Update()" code would run 30 times per second. I understand the "Time.deltaTime" use, but I don't really want to rely on it. I want to use frames like I have everything written in my game's design document. So can you specifically set a framerate, and if so, where/how do you do such a thing?
Also, I'd planned on using my in-game units as inches, but I also don't know how much Unity moves with "transform.Translate". Does it move 1 pixel if I use "transform.Translate(1,0,0)"? Or is it using some different measurement? Clarification would help a lot!
Thanks!
Physics should be handled in FixedUpdate(), not Update(). FixedUpdate() runs synched to time, while Update() only runs every frame.
Answer by Joshua · Jul 30, 2011 at 05:04 AM
The first result in a search for 'Unity3d fixed framerate' already answers your question perfectly, I realize you're new to this site but in the future please use the search function.
What you want to use here is Application.targetFrameRate. I don't advice doing this however, as it's far from a hundred percent reliable and means lag does not only affect how the game is 'perceived' but also how it's actually run, and allowing for people to cheat by altering their frame-rate and faking lag.
You say you understand the concept of using Time.deltaTime, why don't you want to rely on it? I see you're new to 3D development, but I assure you this has absolutely now downside, only upsides.
If you however insist on doing everything in a fixed timestep, don't use Update() but use FixedUpdate(). You can set the fixed time step for this function under Edit > Project Settings > Time > Fixed TimeStep. This means all physics calculations will also be run more or less frequently, so be careful with this. It's also designed to 'catch' missed iterations, which it will que up and run if there was a moment's lag due to a garbage collector spike for instance.
As for using inches for your in-game units, that's completely up to you. The units Unity uses are only relative to eachother, so you can define them as you want to. The only place you have to tweak something is the gravity, as it's by default set to -9.81, which implies meters/second. If you change that, or keep it in mind when using physics, you can just as well use inches/second_on_mars.
Good luck with your project and welcome to Unity ;)
Answer by Waz · Jul 30, 2011 at 04:59 AM
You cannot set the display framerate, simply because you cannot force a computer to go faster.
You can set the physics frame rate in ProjectSetting -> Physics.
Sorry for being rude, but that's simply wrong. You can set a target framerate, that only won't be met if the computers specs are either to low (which you can catch by adjusting quality, etc) or there's a sudden lag.
You've listed exactly why it can only ever be a "target" framerate, I never said "target", and in your own answer you also repeat that it is inaccurate.
This is why we use Time.deltaTime. If we don't, then every frame miss causes an ugly jerk in the animation, and users on slow computers that your "quality adjustments" weren't enough for get a completely different, slow-motion experience. Not to mention users with faster computers complaining.
But computers that are too slow will get this "completely different, slow-motion experience" anyway, as the entire physics system is based on a fixed timestep.
Unity will keep running fixed frames and starving the display framerate if it has to - a good reason to not do too much in FixedUpdate. By comparison:
transform.position += velocity * Time.deltaTime;
takes the same amount of time to execute regardless of framerate, and that's why we do it. Physics is too expensive to do with continuous functions and far cheaper to do discretely, hence the need for FixedUpdate.
When someone asks here "How do I do X", we have a duty not just to tell them the literal answer, but to give the reasons not to do X too. Your answer does that just fine.
Answer by Waz · Jul 30, 2011 at 05:02 AM
If you use something other than meters, you will need to change Gravity accordingly. A few things don't work quite right with non-meter dimensions. One is the TreeCreator, which assumes meters given the limited unit ranges it allows.
Answer by SirMacJefferson · Jul 30, 2011 at 05:40 AM
Thanks for the answers,
Regarding some of the things said:
The first result in a search for 'Unity3d fixed framerate' already answers your question perfectly, I realize you're new to this site but in the future please use the search function.
I searched Google for it a few times with various wording, but I kept getting things like "How to show the user's framerate" rather than fixing the framerate specifically. The manual/reference didn't seem to offer help either (not saying they're bad documents because of that).
You say you understand the concept of using Time.deltaTime, why don't you want to rely on it? I see you're new to 3D development, but I assure you this has absolutely now downside, only upsides
The explanation (which isn't very justified) was that in my GDD, some things (such as jumping) would move X units/frame for 18 frames..I guess I'll just translate them into seconds instead of frames.
EDIT-Sorry, forgot something:
Something I don't understand, however, is what a 'meter' is, specifically. Sorry, I've gotten used to moving things by pixels in 2D games (X + 10 would be 10 pixels to the right), but Unity seems to do things differently. I don't really understand how the gravity would relate to this..? Is the gravity the amount of pixels each meter takes you?
Thanks again for the help!
Your answer
Follow this Question
Related Questions
How does Time.deltaTime provide smoother physics functions? (Frame rate question) 2 Answers
Can I make physics engine speed dependent on frame rate? 2 Answers
Lower Frame rate for less polys ? 1 Answer
Experiencing strange problems with Mathf.SmoothDamp function 2 Answers
Retina specific framerate drop on specific function 1 Answer