Hello this is Craig and I’m going to teach you how to make a Minecraft like environment here in unity this may be this will definitely be more than one video so there are actually some libraries you can use for this but I recommend against it every minecraft Like has its own ideals and visions and if you use one of the basic libraries that you can find on the Internet you won’t know how to make those visions of reality it’s better to understand the engine from the ground up and it’s a pretty simple concept so I’m going to Teach it to you now this is just a new project there’s nothing special about it the only thing I added to it is this image which is just some random scratchings to give our environment some texture so that’s it nothing new we’re going to start now first off is we’re Going to go ahead and import some basic unity assets so we want the character controller just import it all and we want the I’ll be moving a little bit fast by the way because I have a pretty strict time limit because I like things to be pretty will import the skyboxes it Turns out that fraps can’t record this unity editor so i’m using camstudio camstudio has some pretty strict size limits alright so what we want is this first person controller this will be our character oh wait this has a camera in it so delete the camera that unity Starts with you don’t need it but this is the camera we want to put our skybox on so let’s go ahead and add our rendering skybox there it is and let’s go ahead and go down to this into skyboxes and grab one there we go now if we hit play Oh Skybox we’re actually now falling through midair there’s nothing to catch us so let’s go ahead and take a look where are we we are here this is kind of in the middle of nowhere let’s put ourselves at 0 0 0 and now let’s go ahead and create a brick and let’s put It at 0 0 0 oh we’re on top of each other let’s put this guy above the brick now when we hit play we’ll be standing on a brick the physics are already taken care of but that is one hell of a dark brick so let’s go ahead and make it Brighter first thing we need is a material so let’s go ahead create material we’ll call it a tile mat and we will go ahead and drag the tiles texture onto it and then drag it onto the brick oh look how dark it is let’s go ahead And add ourselves a light there you go hit play perfect that’s all of the unity built-in assets we need from here on out everything is custom so I hope you’re ready for it so let’s create ourselves a new c-sharp script we could name it anything and maybe you could do it in JavaScript I wouldn’t recommend it so let’s call it a chunk we probably won’t get into why but chunk is a better name for it and we’ll just drag it onto the cube there it is so let’s open our chunk up I’m using monodevelop monodevelop is the included developing kit it’s not Absolutely terrible so it serves so the first thing to realize about all of those beautiful minecraft worlds is that those bricks are not individual objects that would be crazy you’d have millions and millions of independent objects instead each brick is usually identified by a very very small number of states so For example in this case we’ll be using a byte so each brick is going to be represented by one byte that’s a number between 0 and 256 public byte map there you go we’re also going to go ahead and create a width which we’ll make default to 20 That will be the size of the world we create so in here we go math equals new byte width width width and I always forget I think you need that let’s take a look yep and then you go ahead and do for INT x equals zero X is less than with X plus Plus this is initializing our array here c-sharp is kind enough to initialize it all to zeros so we’ll make 0 empty space and we’ll add in some actual solid space now here you have to make a decision as to whether or not you’re going to go by Unity XYZ coordinates or if you’re going to use more sensible coordinates we’re going to use unity coordinates which means this is actually a Z not a Y we won’t go into why but basically we’re going to make the lowest layer of this group we’re going to make it all solid So map X 0 Z equals 1 so now we have a bottom layer that is a solid bricks let’s make the next layer up kind of choppy just for textures sake so map X 1 Z equals math if dot round-2 int random value now this will give us a number Between 0 & 1 unfortunately we need a byte and it will actually complain if you don’t cast that because an int and a byte are not the same thing so we’ll just cast it there we go so you won’t be able to tell because if I go back here and hit play nothing Happens oh I I always I never remember which way it is with these there we are so here we are if we hit play there’s no difference all we’ve really done is define a map we haven’t actually used it what we need to do is use it all right now here is the Heart of the matter first off we’re going to do some best practices stuff require component type of mesh filter this means that the brick in question requires you to have a mesh filter attached to it now we do have a mesh filter attached to it it is right there So that’s because we’re going to be building a mesh so let’s go ahead and create ourselves a mesh protected mesh and then here we’re going to say mesh equals new mesh and then we’re going to say I guess we should say getcomponent mesh filter and we’re guaranteed to have one because of that Require component up at the top dot mesh equals mesh how exciting so if you want to see what we just did we just broke the brick it’s not there anymore so you can see that the brick is still here in scene edit view and we’re still using The box Collider so we can still stand on it but there’s no longer a mesh for it well it’s time to teach you how to assemble a mesh here in unity it’s actually pretty straightforward we’re going to use a bit of a shortcut that is maybe not best practices but it is Decent enough for our purposes so we’re going to create protected list vector3 verts it equals new and this will be our list of vertexes but a mesh is not made of vertex is alone they also need protected list int triangles and just a future-proof it a little bit protected List vector to UVs so the verts are the pieces of our mesh the triangles are how this pieces connect up and the UVs are what part of the texture to display so we’re going to go ahead and create a new function which we will call regenerate We’ll put that down here come on you public void regenerate so here is where we do our work verts top clear try as clear this doesn’t matter now because we’re starting off with them clear but if we ever want to make a change and then rolled let’s run this again we’re Going to need to get rid of the old data also unity is a little bit picky if you assign a new set of vertexes and your old set of triangles is applied it’s probably going to throw some errors so go ahead and say mesh triangles equals Tries to array and that will clear out all of our triangles because tries is now empty and we’ve just set it to be an empty array alright so now what we have to do is actually generate all of the faces we need we’re actually going to work on generating individual faces now I know some engines work on generating the the framework and then connecting it up with faces using the triangles but we’re actually going to create four distinct vertexes for every single face and that means that if you have a block that sticks up out of the ground you’re Going to be wasting some faces and you’re also going to you can be wasting faces regardless but I mean I’m sorry you’re going to be wasting vertexes regardless but optimizing it at this point would be kind of annoying so we’ll do it by creating all of the faces every Single face will have four vertexes there will be no faces we shared vertexes what am I talking about you’re asking shut up you’re making no sense all right this is what I’m talking about so let’s go back up here this looks familiar grab this crap we need it done here but In this case we also need why we might as well go ahead and make it so that it reads right so what do we need to do alright easy enough bite block equals map X Y Z woo exciting right now if block equals zero continue we don’t need to create Anything that doesn’t exist so if we get here we need to probably create something but maybe not how do we know which faces to create I don’t know let’s go ahead and create a new function to determine that public bool is transparent int X int Y int comma on Z I Get a lot of slowdown using camstudio here so well how do we do this is transparent stuff if let’s put in some safety X is less than 0 or Y is less than 0 or Z is less than 0 or X is greater than or equal to width or Y is Greater than equal to width or Z is greater than or equal to width don’t return now you could either make this false or true if you make it true that means are going to be drawing boundaries at the edge of every block otherwise it means you’re not we’re Going to go ahead and make it true so we’ve just said is if you’re asking for something that’s not on our map we consider it to be transparent ie not there so then we say return block oh sorry map X Y Z equals 0 and that just says if It’s 0 is transparent otherwise it’s not in the long run we’ll have to make that a lot more complicated but for now that’ll work fine so now we say if let’s go ahead and split this off into its own function – lets go function crazy draw Brick X Y Z block so we’ll go ahead and put that I tend to put in alphabetical order so let’s go put it here draw Rick int X int y int Z bite block alright so there are six one two three four five six possible faces we could be drawing let’s Go ahead and draw them so what we’re going to need to do is we’re going to need to define vector three corner we’ll call this start equals new vector three XYZ we’ll need these two for later now let’s go ahead and do some work if is Transparent X y -1 Z so if it’s transparent above us then we need to draw a face above us right except in this case it’s actually below us but that’s okay either way it works fine so since we’re doing that will do offset one equals well we need to do it if it’s At the top we need to draw that face along the x axis and the z axis because the y axis is the one that we’re offset by so there we go this is the x axis this is the z axis so now we’re going to make yet another function I hope you Haven’t lost track yet rakh yeah let’s go ahead and pass it to Brock so public boy come on you it’s like a hundred degrees in here sorry if you’re wondering why there’s so much noise in the background it’s because it’s 100 degrees in here you may be wondering why I’m passing That block around we’ll need it for UV mapping we probably won’t even get there in this video I’m certain we won’t get there in this video all right so if we need to draw a face then what we need to actually do is create four new vector Threes so those vector threes are verts dot a ad start here they are all four of them offset one offset to offset one for this offset to rule so now we actually have the vertexes but are we forgetting something like a face actually this is actually a little bit e I do this Because it’s just a little bit easier to do it like this so a face these triangles are triangles that connect three vertexes we have four vertexes so we need two triangles that means we need six integers the way a triangle works is it lists the number of The vertex involved so if we just started this would be vertex zero vertex 1 vertex 2 and vertex 3 so we need to go ahead and connect them which would be something like this now I can never remember exactly which direction it is in my head because if you do in the Wrong direction they only show up pointing in the wrong way a triangles are they don’t have a back face so if you get them wrong then you will end up with something that looks like cute Oh F Durer I can’t show you yet you will probably End up with a very spotty I’ll show you in a minute ah that was kind of silly of me so regenerate remember this function here we’re doing all of the work after we’ve drawn all of the bricks we need to do mesh dot vertexes equals verts to array Mesh triangles equals triangles dot to array I forgot that part sorry about that we also for future proofing need to go ahead and recalculate those normals we’re going to need them look at that I think I guessed right so apparently that is the correct set of order for those triangles to be added And I was going to show you what the heck I’ll go ahead and break it so I can show you if we change the order here like this and hit play then as you can see we’ve got some faces that are visible from above and some that are Visible from below and that’s kind of a pain in the ass so make sure you get that right or else you will feel foolish actually yeah that is right so I just drew at the bottom so believe it or not this is kind of wrong I’ll Show you why here go ahead and create we want to do the top now right so let’s go ahead and see how that looks whoa okay let’s pause so I’m in scene view right now and you can see that we’ve got two layers we’ve got the lower layer and the Upper layer but wait a minute we can see the lower layer from the top it’s not working out that lower layers pointed the wrong direction we should only be able to see it from beneath so what’s wrong is that we’ve passed it vector three dot right when we should in fact Be passing it vector three dot left but if we do it like that and just like that then we’ll end up offset in the wrong direction so we need to actually add an extra three dot right like this so in case I just lost you what I’ve done is Fallen off the map what I’ve done here in scene view so I haven’t created any of the walls yet I’ve only created the tops so here you can see that it’s a little bit difficult to see because of there’s not being any walls but you’ve got two bricks on the bottom and then You’ve got two bricks above that bottom layer is all filled in but the top layer is spotty remember how we created it like that so here we can see the parts where the top layer is being drawn there’s no face for the layer beneath it see that this brick Does exist this brick down here does exist but you can’t see the top face so don’t draw bother to draw it if we go down below you can see that we have the entire bottom drawn out because this is the bottom there’s nothing beneath it so All of those faces need to be drawn but we a you may be asking that’s an awfully thin brick you’re right that is too thin we do in fact need to take this here and add so now when we fall off again now in scene view when we look at it you can See that the bricks have a serious thickness to them they are thick now let’s go ahead and go and just fill in the other walls real quick here so we do that by simply saying copy paste paste exciting delete X minus one delete X plus one Delete Z minus one delete Z plus one now the problem is we’ve got all this arranged for the y-axis and we no longer need it for the y-axis so here in the x-axis for example here we’ve got a left but we’re already to the left so we Can’t go any further left or else we’d screw up the brick this actually needs to be up because we need to draw the left wall up and back this here needs to be down because we need to draw the right wall down actually I believe that That’s backwards I need this to be down and this to be I’m sorry we need to be this to be down this to be up and so here where we’ve got this as right we need to be up and here we’ve got this being up we need it to be What is it forgotten all the sudden left yeah did I get this backwards with the X and the Y’s I think I might have let’s go ahead and take a look the z-axis is going to be crazy right now so let’s go ahead and comment it out we don’t want to get distracted Alright so did we get the x-axis is correctly no we didn’t look at that what a sad state of affairs so sometimes you run into something like this and it’s really easy to start getting your head all twisted up and you’re like wait which one is which You just got to break it down and make sure you understand which faces are drawing incorrectly in this case the problem is that we are drawing the faces too far to the side so we need to let’s go ahead and make sure that each of these faces independently is drawing Perfectly so if we go back into unity and press play play you stupid now do we have any look there they are there on the right spot but they’re pointed the wrong direction no problem right it’s a pointed the wrong direction then that means we’ve got the wrong one Of these and instead of down we want up and we get rid of this is that all we needed to do for that one let’s go ahead and hit play again find out scene look at that perfect so now let’s go ahead and do the other wall so this needs to Be down which means that this needs to be white and this needs to be up now if you were doing this your first time you probably have to experiment with that now I’m sure that there are other ways to you know there’s this good ways to memorize this and figure it out Perfectly but I want you to really understand that even when it feels complicated it’s not you just have to isolate which faces you’re screwing up and try again so in this case now both the X faces work and we’ll move on to the Z faces maybe we’ll get lucky and They’ll both work first time as if I’d been doing this before so here we don’t need to draw back any further because the back or the front instead what we need to do is draw up this is probably down so let’s see if our luck holds out shall we Almost but not quite all of the Z planes are offset to the wrong side it looks like alright they owe as I said no editing so you have to deal with this kind of crazy stupidity that I sometimes get I’d like to claim that every time I Do it it gets easier but I always forget which faces are supposed to be offset by which amounts is just a thing tile that never managed to figure out oh you know what it is watch that be wrong that was wrong right now anyone who’s done this before Is like you’re kind of an idiot I’m like yeah I know sorry Wow we pushed it even further away from being correct we’re so talented let me go ahead and pause it and I will come back once I’ve done this voila so if we cruise around we can see that This is a fully functional map but there are some things missing what things well for starters Oh No unity makes this part really easy this is the big reason to use unity for this see this boxed lighter we don’t need to get rid of it instead what we need is a Mesh Collider there it is so over here remove this require component we need another one we’re going to want to make a reference to that because we’re going to use it and we’ll go ahead and fill that right here all right so now we’ve set up our Mesh Collider to be our mesh Collider and down here what we’re going to do I’m sure there’s some way to make that work without setting it to null first but setting it to null first works so what happens is this will automatically force it to recalculate every single Time so now we’ll go back into unity and we’ll fall through right away oh you know where that is because we’re inside of the bricks maybe are maybe are oh isn’t that easy now you may be wondering what about the texture what about editing stuff what about this What about that I’m out of time and space on this camstudio recording so I’m hoping that you will go ahead and tune in next time for the rest Video Information
This video, titled ‘Tutorial: Minecraft in Unity #1 (Mesh Generation)’, was uploaded by Craig Perko on 2013-05-31 23:16:38. It has garnered 37104 views and 226 likes. The duration of the video is 00:27:31 or 1651 seconds.
This is the old tutorial. Go to the new one:
How to make a Minecraftlike in Unity, part 1: set up and basic terrain drawing. Also included: forgetting which way is left and which way is right for like five minutes. BRILLIANCE.
… I do get better at presenting.