4
votes

I am using the following code to calculate Frame Rate in Unity3d 4.0. It's applied on a NGUI label.

void Update () {
        timeleft -= Time.deltaTime;
        accum += Time.timeScale/Time.deltaTime;
        ++frames;

        // Interval ended - update GUI text and start new interval
        if( timeleft <= 0.0 )
        {
            // display two fractional digits (f2 format)
            float fps = accum/frames;
            string format = System.String.Format("{0:F2} FPS",fps);
            FPSLabel.text = format;
                timeleft = updateInterval;
                accum = 0.0F;
                frames = 0;
            }
    }

It was working previously, or at least seemed to be working.Then I was having some problem with physics, so I changed the fixed timestep to 0.005 and the Max timestep to 0.017 . Yeah I know it's too low, but my game is working fine on that.
Now the problem is the above FPS code returns 58.82 all the time. I've checked on separate devices (Android). It just doesn't budge. I thought it might be correct, but when I saw profiler, I can clearly see ups and downs there. So obviously it's something fishy.

Am I doing something wrong? I copied the code from somewhere (must be from the script wiki). Is there any other way to know the correct FPS?

By taking cues from this questions, I've tried all methods in the first answer. Even the following code is returning a constant 58.82 FPS. It's happening in the android device only. In the editor I can see fps difference.

float fps = 1.0f/Time.deltaTime;

So I checked the value of Time.deltaTime and it's 0.017 constant in the device. How can this be possible :-/

1

1 Answers

3
votes

It seems to me that the fps counter is correct and the FPS of 58.82 is caused by the changes in your physics time settings. The physics engine probably cannot finish its computation in the available timestep (0.005, which is very low), and that means it will keep computing until it reaches the maximum timestep, which in your case is 0.017. That means all frames will take 0.017 plus any other overhead from rendering / scripts you may have. And 1 / 0.017 equals exactly 58.82.

Maybe you can fix any problems you have with the physics in other ways, without lowering the fixed timestep so much.