I know it’s been said that game objects that are offscreen do not take up a noticeable amount of processing…but in my case it appears they do.
My dungeon is pretty large (made of up 700 total game objects) and it’s made of several regular boxes with simple textures and box colliders. All have baked shadows. When I test on iPhone I get around 15-25 fps. When I delete 75% of the dungeon and try again, I stay around 25-30. My draw counts are pretty low…below 100 batching around 20.
In order to fix this problem I made a script that runs every 2 seconds to measure a given game objects distance from the player and turn it on or off. This actually works great. I stay at 30 fps (my target fps) when I do this. What gives here?
areaFixer+=Time.deltaTime;
if(areaFixer>2){
areaFixer=0;
var child3:Transform;
for(child3 in dungeon.transform){
if(child3.gameObject.GetComponent(Renderer)!=null){
var dist2 = u.distanceTwoPoints(p.transform.position.x, child3.gameObject.transform.position.x, p.transform.position.z, child3.gameObject.transform.position.z);
if(dist2<100){
child3.gameObject.SetActiveRecursively(true);
}else{
child3.gameObject.SetActiveRecursively(false);
}
}
if(child3.name=="coffin" || child3.name=="Chest"){
var child4:Transform;
for(child4 in child3.transform){
var dist3 = u.distanceTwoPoints(p.transform.position.x, child3.gameObject.transform.position.x, p.transform.position.z, child3.gameObject.transform.position.z);
if(dist3<100){
child4.gameObject.SetActiveRecursively(true);
}else{
child4.gameObject.SetActiveRecursively(false);
}
}
}
}
}
Am I setting things up a different way than other people do? Haven’t other people had this problem? Surprised I haven’t heard more talk about it. Anyways, let me know what you think.
Thanks,
Jason
Here’s a pic. Maybe something here will lend some insight…
my bet would be that the box colliders are the issue. the physics calculations might run for all colliders in your level at any given time. you can narrow the problem down if you change your script to disable only the colliders, not the whole gameobject. also, you could use OnBecameVisible() and OnBecameInvisible() to do this check. no need to calculate distances. and if you have unity pro, check the profiler! it’s worth gold.
Hey woodn. Did some experimentation after your post and sure enough, it worked at 30 fps with only the colliders turning on and off according to distance. Thanks! Why doesn’t everybody use this trick if it’s so helpful to processing? Strange…
About the OnBecomeVisible suggestion. That would mean I would have to put a script on all 700 things. Wouldn’t that be super hampering to processor? I’m a bit new to this, is there some way to manage these scripts in a properly efficient way?
Also, the code above is a bit inefficient. I ended up moving all objects into Lists at the Start()…then going through the lists to enable and disable the colliders every 2 seconds.
If you have a script with the OnBecameVisible event only (not Update or FixedUpdate methods), it will probably be fine with 700 of them…you were using Distance functions for each of the 700 anyway it looks like, and Distance functions are a little slow themselves. If you don’t want to put a script on all 700, then at least change to using the SqrMagnitude method of Vector3 instead. It’s much faster. Since the OnBecamevisible event is a built-in one, it’s probably called whether or not you’re using it, so you may as well “catch” the event. The script will need to be on the same prefab (or subprefab) that contains the renderer for it to trigger.
You could also try calling rigidbody.sleep when things leave the screen.
Colliders will result in processing whenever there’s a potential that they could be collided with, regardless of whether they’re on screen or not.
While it depends heavily on your scene’s configuration I’m surprised that it’s made a noticeable difference. In one of my games I have maps made of 1m tiles, each with their own colliders, and increasing the map size has no noticeable effect on performance. Physics systems are really efficient at that kind of thing, usually.
The larger your colliders, though, the harder they are to cull in the “broad phase” because it’s much harder to quickly rule out when things might be able to collide with them in the finer phases. In my case, because most colliders fit within their 1x1m cells it’s really easy for the physics system to cull the vast majority of them out.