Hi,
Is it possible to create a game like terraria using unity? Would it be best to use 2d or 3d project if so?
I’ve already done this in Java with OpenGL, just wondering if it is feasible to do in unity.
Thanks
Hi,
Is it possible to create a game like terraria using unity? Would it be best to use 2d or 3d project if so?
I’ve already done this in Java with OpenGL, just wondering if it is feasible to do in unity.
Thanks
Absolutely. Terraria is a tile-based game, so search for Unity tile map assets/tutorials. But if you want to go beyond Terraria and make something even cooler, please see PixelSurface, which lets you create/modify/destroy the world at the pixel level (forum thread here).
Many thanks.
I was hoping to code something myself a long the lines of using Mesh object and creating it procedurally:
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
void Start () {
mesh = GetComponent<MeshFilter> ().mesh;
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
newVertices.Add( new Vector3 (x , y , z ));
newVertices.Add( new Vector3 (x + 1 , y , z ));
newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
newVertices.Add( new Vector3 (x , y-1 , z ));
newTriangles.AddRange(new int[]{
0, 1, 3, // Triangle 1
1, 2, 3 // Triangle 2
});
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
}
This just simply draws a quad, this would be the tile. Now I would add these to a static array, does this seem a way to go? The above could be used in 3d voxel situations also.
Thanks
Yes, of course if you just want to make a quad, there is a built-in primitive for that.
But by extending the code above, you can create many quads in the same mesh, and set their UV values to appropriate locations in a texture map, to display the desired texture on each one.
That’s the basic idea behind tiling… a bit too advanced for most beginners, but I can see that it will not be much trouble for you!
Great ![]()
So, would I use the built in quad primitive instead or keep using the code I’ve done above?
What I’m planning on doing is storing these ‘quads/tiles’ in a big static 2d array then displaying this array to the screen. Of course, would only render what the camera can see - convert camera space to this ‘2d array’ space.
Thanks
Well, sure, that’s one way to do it. But it won’t be as efficient as making a single mesh that has all the quads in it at once (i.e., if you have R rows and C columns, you’d make a mesh with RC2 triangles, and RC4 vertices).
Can you not add the quad primitive to this ‘mesh’?
Thanks
No. And no need to put “mesh” in quotes; I’m not using it metaphorically.
I’m talking about the Mesh class that you already demonstrated use of above.
Your code generated a mesh with 4 vertices and 2 triangles. Now, just put in some for-loops, and generate a mesh with (say) 400 vertices and 200 triangles.
Or you can just throw a bunch of quad primitives up on the screen. But that won’t perform very well — it’s an awful lot of overhead compared to using a single mesh.
Thank you.
So, let me get this, the mesh object consists of one to many of the quads I’ve coded up, the ‘terraria’ style game would be made up of many of these mesh objects - kind of like 2d chunks?
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
Hmm. In a 3D Minecraft-style world, yes, that’s how you would do it, because the camera can potentially turn to see quite a long distance away.
But in a 2D world, the camera can only show so many rows and columns at once, and that’s it. So you could really get by with only one mesh. There’s no point in even having meshes for all the stuff that’s offscreen; there is no way that stuff could ever be drawn.
So, I would have just one mesh and simply change the textures according to where the camera is.
But I don’t want to discourage you — remember, if you feel you’re getting bogged down in the details and you just want to get on with creating your game, there are several assets in the asset store that already do all this for you. (And there may be some free open-source code out there to do it too; I haven’t looked.)
Hi, thanks again.
One mesh, hmmmm, interesting, so, when these tiles scroll to the left for instance (Camera moves), we just update the mesh textures?
This is what I want (This is my sandbox terrarria type game I did in Java:)
Yes, I think you’ve got the idea.
Ok, so, each one of those quads in the texture would have some way of knowing what texture to apply to them, guess would need some big 2d array to hold texture type?
Yep! (You’ll need that information anyway for your game logic.)
Thanks again ![]()
Would it be possible to have one big mesh with all these quads or would unity render the whole of this mesh? I’m also taking it I set the project up as a 2D one ![]()
If it does render the whole lot, obviously performance would be bad so have:
Does this sound correct?
You mean, rather than having the mesh about the size of the screen, make it the size of the whole world? Well that’s possible, if your world is small enough. Meshes are limited to about 65,000 vertices, so you can do the math to figure out how big that would be.
Probably. The only difference between a 2D project and a 3D project is the default for how textures are imported, and the default scene view mode. So it really doesn’t matter much.
Yes, that’s how I’d do it.
Have you looked for a tutorial showing how to do this? It’s a pretty common need; I’d be shocked if there isn’t something already out that that lays it out clearly.
A quick google search turned up quite a few things that sound promising, but most of them are YouTube videos, which I don’t have time for.
If there really isn’t anything out there, let me know. Perhaps I’ll write it up for my blog.
Many thanks for your reply.
One big mesh covering the whole world would work out to 16000 quads so not really that big.
Want something along the lines of 4096x2048
So looks like one Mesh the size of the screen area and
update this every frame. Guess when camera has moved tile width amount update the mesh so as to get smooth scrolling? Thus move right, if done 32 pixels, then time to update the mesh, as without this, it would just scroll tile width amount of times on each scroll, or am I missing something here?!
Thanks again
Hi,
I’ve looked up on the web, did find this: http://catlikecoding.com/unity/tutorials/procedural-grid/
I guess my next concern is how to scroll the mesh.
Thanks
That’s a good one, though note that the IEnumerator thing isn’t needed — he’s only using that so he can slow it way down, so you can actually watch the mesh being created. A neat trick, but in a real app you could just generate your mesh all at once in Start (or some method called from Start).
I suggest you get that much working first. Then deal with scrolling an entire tile at a time, by updating the UVs. Then worry about sub-tile scrolling. It won’t be that hard — you’ll just shift the mesh over a few pixels (the mesh should be one tile bigger than the screen in each direction). But take it one step at a time.