- Home /
Don't draw cubes not touching air?
How can I make unity 3d not draw cubes that don't touch air(I need better fps), this seems to be to much for me but here is the code that makes a area in game. Please if anyone can even just improve my fps it would be awesome.
public class ResourceSpawn : MonoBehaviour
{
public Transform packIce;
public Transform Ice;
void Start()
{
for (int x = 0; x < 40; x++)
{
for (int y = 0; y > -30; y--)
{
for(int z = 0; z < 40; z++)
{
Instantiate (packIce, new Vector3(x, y, z), Quaternion.identity);
}
}
}
iceSpike();
}
Also should I be using start or awake to call this code once? I know start is called once and discarded and awake is called before the game is visible I think. Am I wrong?
Dude, your trying to Instantiate 48000 Objects all at once.
What are you trying to achieve with this? Some more background information on what your trying to do will help us help you in figuring out Optimizations or other techniques for achieving your goal.
The normal way game engines deal with arrays of cube-like structures is to create a single mesh with cube-like elements within the mesh. Each element is called a Voxel. That is what @Eric5h5 is suggesting. Google "Unity3d Voxels" for some good reading. Here are two references to get you started:
http://unitycoder.com/blog/2012/10/18/voxel-resources-engines-for-unity/
http://www.sauropodstudio.com/how-to-make-a-voxel-base-game-in-unity/
There are ways to improving your arrays-of-game-objects solution, but ultimately you will need a Voxel-based solution if you expect any kind of reasonable performance for the number of cubes you are creating.
Voxel systems delete and place blocks by modifying the mesh. @whydoidoit's comment is point out that Unity has a limit of 64$$anonymous$$ vertices per mesh. That means that each game object will have a maximum number of voxels. I'm guess that most voxel systems build their cubes out of 24 vertices (to allow the use of independent uvs for texturing of all sides). That means each game object will have around a maximum of 1700 cubes. You will need multiple meshes/game objects in order to get the 40$$anonymous$$+ cubes you want.
Given what you are doing, it might be possible to play some really elegant games. You really only need voxels for visible cubes. As you are finding out, what you are attempting to do is not easy to do right.
Hiding is easy. You can turn the renderer off:
renderer.enabled = false;
or you can deactivate the whole game object:
gameObject.SetActive(false);
But you are going to have to be able to find which objects to disable. Usually this is done by placing the game objects in an array and then running through the entries in the array. Given your cubes, you can map your array indexes to world positions and avoid going through the whole array.
Answer by Carbongrip · Feb 20, 2014 at 10:28 PM
Ok I am making a new question thats more specific and has a basic idea and performance option!
New thread can be found here http://answers.unity3d.com/questions/645261/creating-objects-and-hiding-when-out-of-range.html
Answer by Eric5h5 · Feb 20, 2014 at 03:44 AM
As always, don't instantiate a zillion separate objects, use the Mesh class to create mesh chunks.
Um what? I don't want them to be a single mesh. They need to be separate. I was planning on having chunks that load and unload with a size of 64 wide and 64 long of cubes at scale 1. I can this happen?
Not a single mesh, a series of mesh chunks, since a mesh can have at max ~65$$anonymous$$ vertices. Each chunk can have many cubes in it, constructed as a single mesh.
Google "$$anonymous$$inecraft Unity" or similar; lots of topics about this already.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Weird FPS Camera 0 Answers
I cant see my gun its clipping through my camera 0 Answers
FPS Camera, Camera and Player conflicting... 1 Answer
Move gameobject pivot 6 Answers
Multiple Cars not working 1 Answer