- Home /
low fps when looking in a direction with many objects
Hello!
I have an annoying problem with my Unity game. My Problem:
I have many objects in my game (cubes, cylinders). When I'm looking with the 1st Person Controller in a direction with less objects, everything is fine^^ High fps.
But when I'm looking in a direction with many objects, the fps decreases to ~5 to 10 fps. I have set the objects by hand, not by script.
So I dont know what's the problem. :(
Hope you can help me.
P.S. Sry 4 grammar (I'm Austrian)
Answer by Azial · Apr 03, 2013 at 04:31 PM
That's normal, because the render engine is rendering everything the camera faces, even if the objects are behind something. With Unity Pro you can setup Occlusion Culling which calculates at runtime wether objects are seen by the camera. I have only the free version, so I made my own "fake occlusion culling". The script is below. Use: make a new gameobject and drag all other objects that causes the framedropping into this gameobject als childs. Now attach the script on the gameobject. If you haven't any important script on your objects you want to cul, then don't check the "soft culling" checkbox, this would deactivate the objects in the distance. If you check this option, only the renderer components on the objects get deactivated in the distance.
var culRange : int = 100;
var softCulling : boolean = false;
function OnEnable() {
for(var toCul : Transform in transform) { //get all objects we want to cul. Place them as childs of the gamobject that's script is attched
TurnOnOff(toCul, false);
CheckRange(toCul, 0);
}
}
function CheckRange(toCul : Transform, waitFor : int) : IEnumerator {
yield WaitForSeconds(waitFor);
var curRange : int = Vector3.Distance(Camera.main.transform.position, toCul.position); //range from the camera to the object
if (curRange < culRange) { //if in range acivate/make visible
TurnOnOff(toCul, true);
} else { //out of range -> deactivate/invisible
TurnOnOff(toCul, false);
}
var checkIn = Mathf.Max(0.5, 5*curRange/culRange); //the next check depends on the range from the camera to the object (for better performance)
//print("Check for " + toCul.name + " in " + checkIn + " seconds."); //for debug
CheckRange(toCul, checkIn);
}
function TurnOnOff(toCul : Transform, state : boolean) {
if (!softCulling) {
toCul.gameObject.SetActive(state); //deactivate the object(s)
} else {
for (var r : Renderer in toCul.GetComponentsInChildren(Renderer)) {
r.enabled = state; //make the object(s) invisible
}
}
}
function OnDrawGizmosSelected () { //draw a debug sphere to visualize the culRange, not needed for functionality
Gizmos.color = Color.yellow;
for(var toCul : Transform in transform) {
Gizmos.DrawWireSphere(toCul.position, culRange);
}
}
Thank you for the hint with the Occlusion Culling :) Now it works fine with high FPS
Hi!
Now it works fine with the Occlusion Culling :) The FPS are much much better than before.
Answer by Statement · Apr 03, 2013 at 04:20 PM
Well, the problem is likely that you have too many draw calls, that you're rendering a lot of triangles or that your shaders are very complex, or a combination thereof.
You need to optimize your game.
http://docs.unity3d.com/Documentation/Manual/OptimizingGraphicsPerformance.html http://docs.unity3d.com/Documentation/Manual/DrawCallBatching.html
What does the stats window say (see button in game window that says "Stats")?
Do you have Unity Pro or Trial and can use the profiler?
Thanks, I think that's the problem. I will take a look on it at weekend and post it, if it was helpfully!
And yes, I have Unity Pro.
Hello!
Here I have 2 pic of the stats window and the profiler:

As you can see the fps are very low when I'm looking in a direction with many objects.
I'm going to try it with a combineCildren script. I hope it will works :-/
You might be better to batch materials/textures to reduce draw calls if it is possible.
Your answer
Follow this Question
Related Questions
Low FPS after loading a scene 1 Answer
Scroll rect on mobile? 0 Answers
Low Fps in a simple game 1 Answer
Blank scene, 2 sprites, 32 FPS 2 Answers
Serious performance issues 0 Answers