3d objects make games run slower?
Hi everyone!
My problem is, that i want to make a 2D android game, and if i use 3D Objects in it cause it looks far better in top down view, the game gets low on fps.
To describe it in detail:
I have a 10x10 map. Each element is a 3D game object. some of them is floor element (simple cube with texture) and some of them is wall (some wall model i created). And even if only 5-7 is enabled at once which is in the sight range the game gets down to 17-21 fps. Without them it runs easy on 60 fps. Does android hates 3D models? Or cellphones are too weak to render them? After 2 days of searches i don't even know what to think.
Hint: I tried to replace both the walls, and the floor elements, to default texture-less cubes (the default white cubes) and it also make the game slow to the same 17-21 fps.
I dont think that its relevant, but here is some code i use to create and store objects:
public float CamSightRange;
public GameObject Wall;
public GameObject[] Floor;
public GameObject Gate;
public GameObject Camera;
public GameObject Player;
private GameObject[] Elements;
private int[] map;
private GameObject Walls;
private GameObject Floors;
// Use this for initialization
void Start ()
{
Walls = new GameObject("Walls");
Floors = new GameObject("Floors");
Walls.transform.parent = transform;
Floors.transform.parent = transform;
GenerateArray();
GenerateMap();
}
...
// i generate some 10x10 map as 1D [100] int array in the GenerateArray method
...
private void GenerateMap()
{
Elements = new GameObject[102];
for (int i = 0; i < 100; i++)
{
if (map[i] == 0)
{
Elements[i] = GameObject.Instantiate(Wall, new Vector3(i % 10 * 10 + i * 0.002f, -i / 10 * 10, -0.6f + i * 0.001f), Quaternion.LookRotation(Vector3.back));
Elements[i].transform.parent = Walls.transform;
Elements[i].transform.Rotate (new Vector3(0,0,(Random.Range(0,4))*90));
} else
if (map[i] == 1)
{
Elements[i] = GameObject.Instantiate(Floor[Random.Range(0,Floor.Length)], transform);
Elements[i].transform.position = new Vector3(i % 10 * 10, -i / 10 * 10, 0);
Elements[i].transform.Rotate(new Vector3(0, 0, (Random.Range(0, 4)) * 90));
Elements[i].transform.parent = Floors.transform;
}
else
if (map[i] == 2)
{
Elements[i] = GameObject.Instantiate(Gate, transform);
Elements[i].transform.position = new Vector3(i % 10 * 10, -i / 10 * 10, -0.502f);
Elements[i].transform.parent = transform;
}
}
Elements[100] = GameObject.Instantiate(Player, transform);
Elements[100].transform.position = new Vector3(lastpos % 10 * 10, -lastpos / 10 * 10-3, -1);
Elements[100].transform.parent = transform;
Elements[101] = GameObject.Instantiate(Camera, transform);
Elements[101].transform.position = new Vector3(lastpos % 10 * 10, -lastpos / 10 * 10 - 3, -3);
Elements[101].transform.parent = transform;
Elements[101].GetComponent<Camera>().target = Elements[100];
}
the parent objects are some empty gameobject which help me to distribute different types of objects.
additional information: If i use sprites ins$$anonymous$$d of the 3d objects it easily works in 60 fps
Answer by OneCept-Games · Jan 06, 2018 at 10:52 AM
1: What do you do in your Update() callback? Sometimes, if you do heavy loops, instantiating of objects etc. it will cause FPS decrease.
2: Are your 3D objects simple cubes, or are they complex models with high amount of vertices?
3: Are your textures 2048x2048 or higher, 512x512 should be enough
1, nothing is called if the player did not moved. If moved the update just enable or disables the object in sightrange. The problem exists when no movement happened so it is not problem.
2, Simple cubes, but the strange thing is, that if i use my models ins$$anonymous$$d of cubes same fps will be shown. (my models are about 20-30 vertices max. so i dont think its more complicated than cubes....)
3, i just compressed my textures to 512x512... so its not problem too...
Answer by meat5000 · Jan 06, 2018 at 02:59 PM
At a glance... as you have heavy usage of Start() which causes a delay to any functions/callbacks such as Update() within the same scripts. You routine looks pretty intensive, perhaps consider spreading out the operations with a coroutine and switching out Instantiation for Object Pooling which will massively help with the Garbage Collection. When a large amount of objects have been Instantiated then destroyed the GC kicks in when it feels like, to clear the mess. This process completely delays the program until the operation is complete. With a moderate amount of objects the delay can be in the order of 100-200ms per time which will obviously destroy your game even if this happened once every 5 minutes, from a user experience point of view.
If you have a heavy amount of objects that constitute the gameworld then using voxels would help. Make sure Object batching is in effect.
Actually i shredded almost 80% of the code, since im almost done with the game base. i have enemies, maps, statistics... And based on the device tests nothing wrong with the functions. start method use less then 0.1 second to count even on my phone.. which is pretty much the target. my only problem is the fps drop caused by the objects... if i find a solution to this problem im about a week or two from publishing
Your answer