- Home /
60FPS but the game seems lagging
Hi, i made a game, not much complex (with graphics or scripts) but nice. I found a script that shows fps in the game :
using UnityEngine;
using System.Collections;
public class fpsshow : MonoBehaviour
{
float deltaTime = 0.0f;
void Update()
{
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
}
void OnGUI()
{
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 100;
style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
GUI.Label(rect, text, style);
}
}
it shows me (in the build) 63-4 to 70 fps never under 60s (the same if I try the game in the Unity editor with stats active) I even turned down the quality settings a bit (but i think that is not that the probem, because the game uses 7% of the cpu and 70mb of ram, I have a gt 920m, which is enough for this game), but sometimes the game seems lagging or stuttering. I don't know why... the camera movement is in the LateUpdate Function, and the player dosen't move with animations but with phyiscs (rigidbody). (my fixed delta time is 0.01 s).
(I even don't know why the "fpsshow" when the game is in the pause menu, timescale 0, shows infinity fps)
Thanks
Answer by tanoshimi · Apr 06, 2017 at 10:17 PM
"camera movement is in the LateUpdate Function, and the player dosen't move with animations but with phyiscs (rigidbody)."
That's probably your problem right there. Assuming your camera is tracking your player, have you tried moving your camera movement to FixedUpdate also?
Thanks for your answer, in the fixedUpdate nothing change, but in the Update function I don't know why is a bit better
the interpolate setting on the rigidbody is for fine "smoothing" of movement. if your still showing trouble. also if you are moving a transform inside Update remember to multiply by time.delta time to prevent faster movement with higher FPS machines.
this is Sort by GC Alloc but... i even saw another problem maybe, that is correlated with the camera render I don't know wich are the problems (caused by)...
@IceEntry_lol Don't think too much about the %. That's just the percentage of the amount of CPU-cycles done for that frame, which were used to do that particular "thing". If there weren't many CPU-cycles done that frame, then the percentages will be higher for the few "things" that took the most CPU-cycles, even though they took their normal amount. You need to compare the "Time ms" between frames, in order to see if there were differences. Click on an actual spike, and try to see what things used more "Time ms" for a spike-frame, than they did for a non-spike frame. Sometimes, a "thing" is only present in the spike frame (and maybe the one frame preceding or following it) but aren't present in other frames. This could be something like garbage collection. What you are showing looks like GC spikes. If you want to know where you're creating garbage, you need to go through every single frame, and find one where some of YOUR code is creating garbage. You cannot fix the stuff from Physics, Animation, Camera and other internal Unity stuff. Not easily, anyway.
Answer by FortisVenaliter · Apr 06, 2017 at 10:32 PM
Depending on your scripts, it could also be the Garbage Collector. If it runs okay most of the time, but occasionally glitches or freezes momentarily, you may be generating memory every frame which then needs to be cleaned up. Check out the profiler's GC Alloc column to determine if this is the problem.
thanks for your answer, yes i think the problem could be that ( If it runs okay most of the time, but occasionally glitches or freezes momentarily, that happens)
Thanks, I found this, (I'm sorry it's the first time that i see those things, https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection-unity-games i found this but I don't understand how to fix) there are many problems with rendering i see.... About the GC alloc i can't read that graphic i don't understand it, but at some points the GC alloc has down peaks.. could be that the problem?
Could be, yeah. To verify, click anywhere on the CPU graph and look at the columns in the below list. Sort by GC Alloc and drill down to see where the problem is co$$anonymous$$g from.
Garbage Collection isn't a Unity specific problem. It applies to all C#/$$anonymous$$ono programs since the memory is handled for you. When something in memory is no longer referenced, it gets put in a pile to be thrown out. When that pile gets big enough (or enough time has passed), the garbage collector will clean it up, which takes time and can cause a brief delay. There are a lot of guides online that talk about reducing garbage collection, but to boil it down to the simplest point: Don't use the 'new' keyword in any function that's called every frame (like Update). That's missing a lot of nuance, but it should get you started.
Your answer
Follow this Question
Related Questions
micro Jittering, stuttering... bug of unity? 2 Answers
Game stuttering only in iPhoneX and above. Not a performance issue. 0 Answers
Laggy 3D game on Android 0 Answers
Odd FPS in Editor & Built game. 0 Answers
Scene and Game Screen Lag 0 Answers