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. :frowning:

Hope you can help me.

P.S. Sry 4 grammar (I’m Austrian)

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);
	}
}

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.

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?