Hello everybody I’m just another game developer and today we’re talking about basic foxhole terrain generation what this basically means is generating boxes randomly how it’s done for loops it’s a very simple way to do it and I’m gonna show you in the code and what you’re gonna get is the complete unity project And a github source package that I will link in the description and knowledge what more could you get if you want to check out my last video which was about random number generation click right here now let’s go hop in a unity alright guys so at the moment you can see that It’s fairly plain and don’t mind these errors they just happen from time to time who knows but uh at the moment you could see nothing’s really happening that’s because we haven’t set the wide depth or the y height the white depth basically saying how deep we want it to Be or how deep we want the limit to be and how high we want the height max to be so as you can see it’ll start to randomly generate the bigger we make it the more it’ll generate you can generate just on the x-axis if you wanted to if You wanted to you can make a 3d type game where don’t mind that I’ll know what that was doing but uh that’s a very interesting thing I’ve never seen that where it just generates randomly along that axis or as I said before you could do 150 by 150 and generate all of this Now it seems like a lot to take on your CPU and it is if you’re looking at it however along with this package I wrote a performance optimization script that is really simple and when you’re not looking at anything it won’t render shadows which believe it or not takes up A lot of CPU usage and GPU usage so when it’s not looking at it it’s not rendering the shadows making your performance better let’s just click the stats here and see what we get this is at 53 right now 50 around there we turn around and it’s at 10 frames so as you Can see the script makes a big difference and you can optimize it even more if you wanted to through script or if you’re just using the camera you could go ahead and bring that down to about fifty that way you’re only rendering what’s fifty meters in Front of you making it a lot better to use now you’re getting frame rates of thirty which is better than ten or if you’re looking down here you’re not even getting that much but uh yeah so let’s take a look in the code and see what’s actually happening under the engine Alright guys so as you know the whole goal was to do this in under 100 lines of code I got it at 95 you do it doesn’t matter let’s get into what is it it’s actually doing so to start off I have a bunch of variables here I have one for The X limit 1 for the Z limit don’t do that he’s basically just saying how far to the right deal on it to go how far forward do you want it to go that will come in later when we when we get into for loops then we’re gonna use Y depth To set how low we want it or how low we want it to be able to be if not it’s just gonna flatten out same with height do we want to spawn trees if so what frequency do you want to do it at then we’re using gameobjects trees and blocks Just to give it that effect then these come in handy with the four loops at awake we’re not doing anything I used to that can just be some more line shaved off of what we had how many lines are we got now 90 so 10 under a hundred I’m sure I’ll find something That doesn’t belong in here as I go on in the update I’m getting rid of the tree clones because we’re making a lot of tree fund since we’re instantiating a lot of trees getting rid of those some reasons the block clones don’t ever do anything so we don’t have to worry about The block moments thank God we just have to delete the tree clones one day as they’re getting generated that way it doesn’t get two copies of the same thing then if we get the jump button downs can generate the terrain what does is generate terrain do you ask this is what It does at first we make a game object array variable that we make anything with the tag terrain put it in this array and then we have a for loop saying to destroy everything in that array if it’s the first time you’re generating anything nothing’s gonna happen because you haven’t generated anything So there’s nothing to do with the tag this is just to do after you generate it a second time or a third time or so on it will delete everything before before it generates anything that way it doesn’t load to at the same time that Gets very very laggy now we have a few inch that will come in handy later I’ll reference those later the X I that was up here this is what I like to call X index as you know with four loops if you don’t know a for loop is for I equal to Whatever value you want it I is less than whatever value will you want it and I goes up by how much the X index is basically the x-axis whatever point it’s at so this is x axis is always going to be set to zero initially because you Want it to render or you want to start generating from x axis on 0 so X I is equal to 0 X I is less than the axial image so if it’s 150 it’s only gonna hunt it’s only gonna generate up to 150 and then it’s gonna go up by 1 each time Inside of the for loop every time it runs we’re making a cube called X cube and we’re spawning it on the integer X whatever whatever value it’s at at that moment it’s gonna spawn on that x point and then we’re making a random number Between last X Y and last X Y plus 2 which is it’s made right here it’s not made right here it’s determined right here so initially it’s not going to matter since it’s the first block nothing matters about the first block it’s just gonna be the default and again If you need if you want to know more about random dot range my last video stuff about random number generation and how borderlands used it to make a bunch of weapons I think it’s like gazillion or whatever click that right here and you’ll be taken to that video then we’re Just doing quaternion that identity basically making the rotation whatever it wants to be since we’re not rotating it at all then we’re making sure that if it’s under the wide depth so if it’s under the lowest point we want it to be at then just put it at the lowest point That we want it to be at same with the height so now when we actually equal that value last X Y we’re getting the position of that cube so that we know not to go too far above or too far below and then we’re gonna make Last see y equal to last X Y you’ll see in a minute and X cube we’re gonna make that tag to reign that way when we want to find all the terrain objects we can destroy them all right here now let’s go into the for Loop within the for loop this uses the Z index that I made same structure as the X index we’re making a Z cube same exact thing Z index is gonna be this one so whatever this is equal to it’s gonna be equal to the Z index it doesn’t matter Then same with the depth and the height since that applies then within this for loop we’re having a while loop so while spawn trees is equal to true make a new int called Foilers gen that generates a random number again same as the last video we’re making a random Number between 0 and whatever to spawn frequency is which is a number between 20 and 50 and then we’re dividing that by 2 because if we had a number of 50 then it would spawn too little if we had a number of 20 it wouldn’t spawn that Much either so I’m dividing that by 2 giving the user some actual customers customizability if they want that so then if foil is gen if this random number is equal to 0 then generate a tree above the block that you just made and make that tree equal to terrain or The tag equal to terrain and we’re breaking it so we don’t put the user in an infinite loop causing that computer to crash then we make Z cube equal the tag equal to terrain and then we’re waiting for a frame that way it doesn’t Load it all at once and just put a huge load on your CPU that’s one optimization thing that’s the whole script I just ran through it in about five minutes that’s the whole script and then we also have this performance optimization script that’s going to be in the package that Gets a mesh renderer called render you make it equal there doesn’t do anything an update you could delete that really if you wanted to woops you delete that if you wanted to and then on become invisible so once the camera doesn’t see it anymore then it’s not going to receive any Shadows which believe it or not takes up a lot of CPU and GPU usage so it helps immensely as you saw before then on become visible start rendering the shadows again to make it look pretty that is pretty much it thank you for watching be sure to subscribe for more How-to is how it’s done and other game development videos Video Information
This video, titled ‘How To – Randomly Generate Terrain (Like Minecraft) in under 100 lines of code!’, was uploaded by Justanothergamedev on 2015-07-18 15:37:04. It has garnered 2805 views and 17 likes. The duration of the video is 00:09:33 or 573 seconds.
Hello, fellow developers! This is a video showcasing a very basic voxel based terrain generator. If you would like, you could download the assets from the GitHub repository and try out the Terrain Generator yourself.
GitHub Project Assets: https://github.com/justanothergamedev/Basic-Voxel-Terrain-Generation