- Home /
How to create wall at runtime?
Hi, I'm fairly new to Unity. I haven't made anything that anyone could call a game, right now I'm just trying different things out seeing how they work. I'm thinking I'm about ready to make my first 'game'.
The concept is very simple, it will be a randomly generated maze that the player has to navigate (first person) to get to the exit point, moving on to the next level.
I'm thinking that a good starting point would be the level generation. I am wondering what the best approach would be.
This is my thought process so far:
Have a plane for the floor, manually setting the Y location so that my wall prefabs fit nicely when they have a y position of 0
Since there would be no movement on the Y axis all I'm concerned with the X and Z locations of the walls.
I thought it would be nice to have thin walls but after considering it, I think it may be easier to have the 'walls' fill a whole cell.
I want the maze to be scalable starting small and getting larger. 100x100 at the maximum (this may be too large to be practical, but just throwing that out there as a hard max).
Using a cube as the wall object, I generated a 100x100 grid filled with 1x1x1 cubes. I noticed a significant performance hit (obviously that is a lot of objects).
I thought about scaling the cubes to reduce the number of objects loaded into the scene (ex: 4 walls in a row along the x axis would be 1 cube scaled out 4, instead of 4 individual cubes). I ran into some positioning problems.
Anyway the point of this thread is to ask what any of you guys would suggest in so far as building these walls. What would be the most inexpensive way to achieve what I'm looking for?
Answer by ScroodgeM · Aug 29, 2012 at 06:45 PM
if you interesting in inexpensive way to draw all these geometry, best way is to generate it procedurally at runtime. this let you do all scene in one drawcall and without unneeded hided geometry like down-faced sides.
not so clean but also good way is to combine meshes in one big mesh.
simple mesh generator - MeshGenerator.cs
using UnityEngine; using System.Collections;
public class MeshGenerator : MonoBehaviour { private GameObject tmpobj; void Start() { tmpobj = new GameObject(); MeshFilter mf = tmpobj.AddComponent();
tmpobj.AddComponent(); tmpobj.name = "Plane"; tmpobj.renderer.material.color = new Color(0.9490f, 0.8901f, 0.7686f, 0);
Vector3[] verts = new Vector3[4]; Vector2[] uv = new Vector2[4]; Vector3[] normals = new Vector3[4]; int[] tri = new int[6];
verts[0] = new Vector3(0f, 0f, 0f); verts[1] = new Vector3(0f, 1f, 0f); verts[2] = new Vector3(1f, 1f, 0f); verts[3] = new Vector3(1f, 0f, 0f);
normals[0] = new Vector3(0f, 0f, 1f); normals[1] = new Vector3(0f, 0f, 1f); normals[2] = new Vector3(0f, 0f, 1f); normals[3] = new Vector3(0f, 0f, 1f);
uv[0] = new Vector2(0f, 0f); uv[1] = new Vector2(0f, 1f); uv[2] = new Vector2(1f, 1f); uv[3] = new Vector2(1f, 0f);
tri[0] = 2; tri[1] = 0; tri[2] = 3;
tri[3] = 1; tri[4] = 0; tri[5] = 2;
Mesh mesh = new Mesh(); mesh.name = "Plane"; mesh.vertices = verts; mesh.triangles = tri; mesh.uv = uv; mesh.normals = normals; mf.mesh = mesh; } }
So if I understand this correctly. i would make a vector3 array containing the edge points of all the walls?
correct. than make triangles to make rendering surfaces. simply all 8)
Thanks for the example! that is fantastic!
i understand the verts, but im confused as to what tri, uv, and normals are
tri is triangle. each triangle should have 3 verts. in this example there are 2 triangles, it's $$anonymous$$imum to cover square.
uv is texture mapping
http://en.wikipedia.org/wiki/Texture_mapping
normals are used for lighting.
http://en.wikipedia.org/wiki/Normal_(geometry)
i'm sure it's a lot of information about it over unity answers and internet. these are basics for 3d modelling.
ask if you still need some explain
Answer by GlitchBait · Aug 29, 2012 at 06:48 PM
It may be easier and provide more control by fabricating each maze manually beforehand, so you can have the player progress through each more advanced maze by moving to the next scene.
Instantiating the maze from empty game objects would look really cool! (having the walls drop into place from far away moving to the player camera and around them.
Edit Instantiating wall sections at a time, not cubes. I should have clearified.
The fabrication of the maze before hand is an option i guess.. I would prefer to avoid it as I would like the randomness (also since im going to be the only one playing the game, it makes it more interesting for me ;) )
Answer by Piflik · Aug 29, 2012 at 06:49 PM
I would probably go the cube way...but delete the top and bottom faces. That would be 8 faces per cube. With 100² cubes, that would be 80k triangles, which is not much (for PC). Also you would never need that many cubes on a 100² grid, since you would only need those left and right of the path. The rest would be empty.
Does the amount of objects in the scene affect performance? Also how would I remove the upper and bottom faces?
You can remove the top and bottom in any modeling program (Blender is free).
Both the amount of objects and the amount of faces affect the performance.
Your answer
Follow this Question
Related Questions
Patching glitches 1 Answer
RigidbodyFPSWalker climbing walls 2 Answers
Moving through walls 4 Answers
Setting camera pitch - Never works? 0 Answers