Hello crafts in unity apologize upfront if I sound terrible somebody made a comment in one of the previous videos about me struggling to speak through the fact that I’m not used to talking on my microphone this week I’m struggling to speak because physically cannot breathe half the time I’m hopped up on cough Sweets and green tea and whatever so if you hear a bit of coughing and wheezing listen horror Allah can do about it and I didn’t want to put this video off any longer than I had to especially after the weight you all had for the previous Video so we’ll get straight into it in this one we’re going to handle chunk generation so in the last one where we left off we had our single chunk generated in the middle there in this video we’re going to generate multiple chunks and have the game handle that Generation rather than I was having it so up here before we get into that I just want to change one little thing I said I think I said that was going to have my air block be 255 in the in this index in the block types that’s not Gonna work because because it’s an array I can’t have a 255 without the previous 254 being there so we’re going to have to how we’re gonna have air block as 0 which i think is how minecraft does if i could be wrong but what that mean it’s Not going to have any bearing on this episode because we’re not going to introduce air blocks just yet the whole chunk is completely solid top to bottom the way we are doing it at the minute but we’re just gonna fix that now while it’s in my add so for just duplicate Bedrock and we’ll change but it doesn’t really matter what these are we’ll just set them all to zero I want to make this one we’ll call it air obviously and the only difference is it’s not solid like I say for now it makes absolutely no difference but it doesn’t mean we’re Going to have to change in here this is no longer 0 this is now 1 and this will be 2 and we’ll leave that 3 because now we shuffled them up that will make that like a grass block as opposed to a furnace block obviously we don’t want it As a furnace block that was just to demonstrate the extra so the first thing we want to do to start young generation is we’re going to get rid of our chunk object because we’re no longer going to be using that and then if we go into our chunk class We’re going to change it it’s no longer going to be a mono behavior because but what is this we don’t we’re gonna have a lot of stuff to keep a track on and the most if there is obviously the the moral system intensive it gets we want to be Able to control all that and it’s a lot easier to control everything if we have it in one place and we don’t want every single chunk mesh to have its own script running doing its own thing that we have to try and keep control of so we’re Going to take all that away from the object the object is just going to be a dumb game object with a mesh it’s not gonna have any script on it whatsoever so obviously we’re all going to copy this because we’re going to need that and then we get rid of the start method Altogether and in its place we’re going to have a constructor and in that constructor we’re going to take in our world reference because if you remember me saying the of the world the game object find function that we were using before is quite expensive relative to Where it is and you don’t want to be using it on a lot of objects and we’re going to be generating a lot of chunks on the fly and it’s it’s an unnecessary like spike they could create so what we’re going to do is we’re going to take In the world reference here and then we don’t have to go looking for it so we can just immediately say world equals world and then we’ve got our world reference and what we have before all that we’re just gonna stick that there so whenever a new chunk is initialized This obviously will run but we’re gonna need a few more values obviously we need a mesh renderer and a mesh filter just take there no longer need to be public we’re also going to need a game object because now we’re not starting with a game object we’re going to create one on The fly so we’ll just call it chunk object but spelled with a J and that will be Wow typing is on point chunk object C equals new game object got there at the end and then for mesh filter it’s going to be chunk object dot add component and then That will both add the component to our new game object and at the same time apply our reference to the to this value that we need and then we do the same for mesh renderer really sorry about the typo blame all the drugs so they’re the References that we need and then we also need a material which I can’t remember is that public yeah so material is here in the world class and its public and since we have a reference to our world class we can get it straight from there so mesh renderer dot material equals World dot material one last thing we’ll do just to keep things nice and neat and tidy is we will set our parent for this so we don’t just have lot of chunks generating in the hierarchy we want them all in one place we’ll set our parent to World transform of course you have to actually use the set parent function so now we want to go into a world class we’re gonna have to create we’ll just get rid of that we don’t look enough for now create a style function and all we really need to do here is We’ll just say chunk chunk equals new chunk that’s pretty much it and we obviously we need to pass in the world and that should be that now we should be back to where we started but now obviously we’re generating that chunk on the fly it’s been put in here On nice and neat and we can work from there so now it’s a lot easier for us to create new chunks on the fly so before we move on there’s a few extra things we’re going to have to create all that I want to create but one of them is a new Class we’re going to call this one touch I think it made more sense if you had it in the chunk class so we will put this down here public I’m gonna call it chunk card and it’ll make more sense as to what that is in a second sorry All currents will be funneled and what this is going to do is is kind of like a vector to Brits holding an int rather than a float we only need two values public int X and public in Z and what this is is it’s the position of the Chunk that we are drawing but not in world space so it’s so if we imagine the chunks were like in Minecraft the chunks are 16 by 16 so a chunk number one would be at zero and then the next chunk will be at 16 and then the chunk after that Would be at 32 and so on this is not that that’s the wood that’s the absolute world space this is the chunk position within the chunk map I guess you’d call it so in this one chunk one would be zero and then the next one would be 1 And the next one will be 2 so this is completely different to the absolute position and again we’re going to need a skin structure to make life way easier Jesus and all that’s gonna be is x equals underscore X Z equals on this flower present and now we want to keep a Reference to this up here so this will make this one public actually just cause we’ll just call this card I want to take in this value here so this is what’s going to tell this chunk where it is in the world you know I actually want that before the world reference that’s the Most important piece of information on the scorecard so god equals underscore card and then what we can do with that is we can position now that we have a reference to some kind of positioning information I know I said that the job guard is not the position information But we can’t get the position information we can work out the position from this so once you’ve set up parent will do chunk object dot transform dot position equals new vector3 then we want card dot X times voxel data dot chunk width we know it’s going to be At zero on the Y position and then we want the same again for the Zed and just to make things be easier to follow along in the in the inspector and everything will just say his name using this data as well just to make things a little little easier to follow Up so now I’m weld obviously we have to pass that information so just to show it working will create any new chunk card 0 0 and then again nothing much will have changed except for the father now it’s it’s got a name and if we go in here and Copy this we put one there I will need to give it a different name now – we should have two chunks right next to each other and they’re appropriately labeled and filed away on the world just the way we want them now obviously we don’t want to have to write this out Every time I want to generate a chunk so we’re going to create a new class now now this function can be quite simple because besa the generate world function will be different to this the code that will write to update the world so as the player moves through the world as more Of chunks are generated and loaded and all that sort of thing that will be different to this this function is just run the ones when the game when the games first launched and when you first thrown into the world that’s the only time that this function will run so we don’t need to Get too complicated with this so what we’re going to before we do this actually sorry we’re going to need a new value in here and that if you haven’t guessed which I think you probably have it’s going to be the world size function and we’re gonna call it world size in Chunks because we’re going to have a world size in blocks or box holes as well so we need to distinguish between the two and for now we’re just gonna set it at 4:05 it’s really not important for us it’s just for testing I don’t want to Set a massive number because it will it is going to take some time to load each time we’d run it and I don’t want you to sit waiting 30 seconds every time I run the game so now that we’ve got that value we need some far loops no head is All over the place X is now less than our new value world size in chalks and he wants to be minus 1 actually they know that it doesn’t it will need to be minus 1 but it doesn’t yep and there’s no point in putting there before you Know why and then I wish they X plus plus and then we will just copy that fuzzy I need to get around to turning off the visual Studios propensity to also arrange my curly brackets and then in here we can just create chalk new chunk equals new new chunk to the new chunk Card of X Z and obviously a reference to the world and we’ll get rid of these and in that place I’ll just call our generate world function so now we know we have an error oh yeah if you’re going to create a new function make sure you close the Brackets on that function so now if we were to catch up and we run this there we have a 5×5 world now made up of these chunks so we’re getting that a bit closer and you can see down here they’re all nicely organized so that when you’re Developing you can get to think you can figure things out easier for particular chunks not showing up the way it is supposed to do you’ll be able to figure out which chunky is without any unnecessary I don’t know counting or something so now we need to start keeping track of Things and this is where the – one thing that I was talking about comes in so what we’re gonna do is we’re going to have a chunks a chunks map kind of thing although is it’s not really a map there’s more of a list I guess you’re Just going to call it chunks and it is going to be the world size in chunks by world sizing chunks it’s a two-dimensional array x and z and then what we’re going to do when we call this here instead of creating a new chunk creating this new junk junk new Junk however we’re going to put it into this array and the X the position in this array will be the X and Z that we are passing to it so the position will correspond to its actual chunk coordinates so we’re going to little chokes X Z equals so again absolutely no Difference when we were on the game but now we have a reference to these chunks now they don’t just exist in the game they also exist in this array and we can find them which is going to be important shortly so now before we move on we’re Just going to do a few little things that will make life a little bit easier going forward so the first thing is we’re going to move this because we are going to have to do a few other things later on when we create a new chunk Other than just this part so we’re going to put this in it’s one little function which will be aptly named create new chunk and we will pass in a and we’ll pass in a Intex and an int Zed cut this out and put it there as I say doesn’t make anything Different any difference now but we are going to have to do some other stuff along with this when we create a new chunk at some point quite soon so I may as well just get this out of the way now we also are going to need a few other Little additional functions one such function is well actually it’s already exist but we’re going to move it into a neater place so we’re gonna take this code here and put that in its own function I’ll just stick it here for now bull is vauxhall in chunk or bus in the 3 int so if it’s below zero or out of bearing in mind this is a local value when we’re talking within the chunk we’re just talking about the voxels in that chunk so zero is the star of the tongue even if that chunk is world position 2058 zero as far as this chunk Is concerned is the left-hand side of the chunk it doesn’t matter where it is in the world so this is all local information that we’re talking about so int X Y Zed locally so if it’s less than 0 or greater than chunk width then we Know it is not in this chunk I want to return false otherwise it is in this chunk and will return true then we’re just gonna replace this if here with if not is in is voxel intial we pass in the XY is that again that makes no difference It’s just a handy function that we are going to need this function for other things now that we have it and then we’re going to do a couple of other little things that will come in in a bit such as we need to be able we’re going To need to be able to turn our chunk on and off so we’ll do that with I’ll just call it is active I guess this one’s going to need to be public I’m just going to need to be a function he’s active actually we’ll do it this way public board is active And we’re going to return the chunk object’s state if anyone asks which is active self so if we ever ask what if we just read is active it will just return the junk objects active self state whereas if we want to set it we need to use the chunk object set active function Jesus passing the value 15 typos later and then just one more we’re gonna have a public back to three position that’s just completely pointless really it just saves us having a type certain stuff out over and over again whenever we need particular information so this position Means that we can access a position data for this chunk rather than going in chunk object transform position every time we need it and this is going to be read-only because we’re not we’re never going to set the chunk position from in here so it’s only going to be a get and It’s going to return chunk object transform position now in here in world going to want a similar function to his foxhole in chunk only it’s going to be his junk in world so we’ll just create a new function oh is chunk in world and we’re going to Have to pass in a value for this one and it’s going to be a chunk card because we have the class so you may as well use it I know it’s going to be is if card X is less is sorry is greater than 0 and X is Less than voxel data dot chunk nope so it will world sizing chunks and and it has to be and for this one oh sorry that needs to be a minus one because now we need to get up there as well this needs to be minus one because we’re working With an array so we don’t go to the full I mean if like if you remember an array of 16 will end at 15 because it starts at 0 so you still got 16 spots but it only goes up to 15 because 0 counts as one of those spots so you need that Minus 1 so if it is all those things it is in the world return true else it is not in the world and we return false and another thing so we also want to be able to check if a Vauxhall is in the world and the way we’re gonna do that is First off we’re gonna create another value in here this one is going to be a int and this was going to be world size in voxels and we’re not going to star this value because we have all the information we need to calculate the value here so rather than store it and Have to change it we’re just gonna calculate it on the fly obviously it’s gonna be read-only because you can’t change this file none of these values should be getting changed during the game so to get that we want to return world size in-jokes times chunk width And then we’re going to create a new function again bull is voxel in world this time we’re going to pass in a vector three and it’s a very similar situation so we’ve got Paz X is less than zero no sorry greater than zero and positive x is less than Box and all date setup world size in foxholes and then again we want and and then we have to repeat for all the other ones so we’re just gonna copy them for now life a little bit easier so again these need to be minus one and obviously We need to change we don’t know just check against X three times in a row X Y and then and this formed because it’s why we’re not checking against the world size we’re checking against chunk height so if it is if this condition much is True then we are in the world and we can return true else return false and then the one final thing we’re gonna do is we are moving our or where the hell is it we’re moving our check announcer it’s an hour check its get Vauxhall which I Appear to have lost oh it’s that of course we haven’t made it yet sorry we’re moving this cold out of here and into its own script is home function sorry in here it’s gonna be public because the chunk needs to be able to get to this code get a voxel we’ll call It and this is going to be the the most important another maze this is this function is going to become the engine that drives your Minecraft world generation thing when we talk about the Minecraft level generation world generation algorithm this function that we’re about to create now is essentially the my Of the algorithm obviously it’s going is starting off it’s going to be very very simple but as it gets more complicated you’ll see how it decides where to put caves where to put a biome or ever all of that will happen in this function there it’s a pass a position and Basically all this functions going to do is we’re going to pass in a position to it and it’s going to give us a voxel ID so it wants to be a byte wherever like wherever that is in the world so we need to change this so instead of adding it To a box on map it’s returning it and of course you don’t need equal signs when you’re returning and then we need to put positive Y under the minute we’re only using the Y value to decide anything but that will change and then if we go back In here we want sorry voxel map X Y Zed now equals world dot get voxel new vector3 X Y Zed now nothing should have changed and we haven’t really added we haven’t really done a right lot in terms of what’s gonna affect the game but I just Want to quickly run it just to make sure there aren’t any typos that I’ve missed or anything like that everything’s still running as it should yeah right okay so we’re not quite there yet as you just noticed when I zoomed in if you remember We we went to a lot of trouble to draw the chunk from the outside as a mesh so that we didn’t have each voxel having all its six sides when you don’t need them you don’t need to see a side that is obscured by another voxel now we have The same problem but now with chunks as you can see if you zoom in here all the chunks are drawn all the way around the outside now we’re going to have to deal with this properly but for this tutorial the easiest way to get around this is first off we’re going To add in here a little check just to make sure that whatever foxhole we our whatever foxhole we’re currently working on is foxhole in world and we’re gonna pass in this pulse if whatever Vauxhall we’re passing in here is outside of the world we’re just going to return zero There’s no point in messing around we don’t need to know anything about information anything about Vauxhall days outside of the world because we’re never going to generate it so the way we get our proper value is we return will dot block types and then the ID that we want Is the the ID from our box we’ll be passing in however the information here this pause value is local only so it’s only going from not to I think we’re set of fifteen at the minute but whatever your chunk with this at this position value is only working within that frame Of reference and the get Vauxhall is worldwide so we need to take that into account and we do that by adding our position value and if you remember if we didn’t have our position this here then we would have had to have typed that out anytime I wanted to reference the Position and then it is solid so we’re looking into the block types class we’re getting our ID from the get Vauxhall thing using this position to figure out what the voxel is and then we’re doing checking the is solid function of that block type only doing this if it is Outside of the junk of his inside the chunk we just get this information straight from the Vauxhall map cause we wouldn’t be able to get this far if we hadn’t generated probably populate the Vox on up so we know that we have information there okay so I had To run off and answer the door and when I came back a brain fart in trying to remember exactly where it was and while I was figuring it out I did notice that there were a few mistakes that we just need to quickly fix so the first thing Is obvious we need to add our position data into here because this is calling the get box or function as I think I mentioned before which is global value so it needs to know where in the world it is it can’t just rely on this local Data so we need to add our position there the other thing is this here this returns true if it is in the way if the voxel low is checking is in the world and obviously we’re on the return in not if it’s out of the world so that wants To be if it’s not in the world and the final mistake is down here this isn’t working with an array so this does not want to be minus one like I was saying apologize for this because that’s just telling us whether it’s in or out of the World is not pulling from the actual array and for the same reason this needs to be if it’s greater than or equal to zero and then with all that being done I think if we press play now once he catches oh we should have our chunks and They should be drawn properly yep so now we were only drawing the edges of the chunks when they are at the edge of the world which we can turn on or off if we wanted to I’m going to leave them on just because it gives us something to Look at and just show that everything’s working fine we’ll change the the world size eight just a different value just to show you that it’s doing what it’s supposed to do and there we have it so that’s fine for a static world but you’re only going to Generate once but these worlds are a lot bigger than five or eight or ten or even 50 chunks so we need to manage this a little bit better so the first thing we’re going to do is we’re going to create ourselves a view distance so what This will be as it sounds is just going to be the amount of chunks with which the player can see so if a chunk is beyond that distance from the player it won’t bother being shown on screen or it won’t generate if it’s creating the chunks in the world generation function It will only show them if they’re within this distance of chunks from the player so we’ll just call it what all it’s our aesthetic static read-only and just in case you’re wondering these values can be rearranged later so that you can have them as options this is just for the Sake of this tutorial we’re having them in this single static read-only class that you would have you write like you chunk with a new chunk height would be a fix they needle really want people changing that but the world size for example in the view distance that’s the Sort of thing that the players should be able to change but for now we’re just gonna keep things nice and simple like this so we’re gonna call this view distance in chunks we’re not going to have a view distance in boxes or it’s just nice to keep everything very Clearly marked so what we’re gonna do is we’re going to change the world size in chunks to a very large number call it a hundred and then we’ll have a view distance be five group so then we need to go into our world class and we need To change well the first thing we need to do is we need to have a transform reference for a player or make this one public actually and for now our player is just gonna be the camera and then we also are going to have a wall make this public as well a Spawn position now our spawn position is quite easy to work out for what what we’re going to do is so you may have the way this is working is we have this giant this potentially giant array which is storing all the chunks and potential and keeping space open for potential Chunks because we’re having this fixed sized array and there are other ways of doing it there are more dynamic that only have arrays the size of the amount of chunks you have and all that I’m choosing to do it this way because it’s a lot simpler to follow along and I Think it feels more efficient than using for example a list I’m not saying it’s the most efficient way to doing to do it but I do think it’s the most simplest way to illustrate how I’m doing it and like I said this is by no means meant to Be a tutorial for people who know what they’re doing this is meant to be very beginners if you if you think this seems basic then you probably already beyond where I am so feel free to go find a much better tutorial this goes more in depth than I am so because we because We’re saving this all this basically our world starts at zero zero in the bottom left hand corner spawn is not zero zero so right now our world is going to be a hundred by 100 chunks so to set spawn we’re just gonna say spawn location equals and then it’s sorry new voxel Data dot world size in chunks / – for the because we’re our chunks are completely solid all the way from top to bottom right now we can just pass in the chunk height for this one obviously when we have more nuanced terrain when we have actual terrain Hills and stuff we Will need to find out the height of the position that we’re going to put the player on but for now we know the height because all the chunks are full and then the Zed is the exact same as the X so that puts us smack bang in the middle of the world But of course it helps if you name it correctly so that puts our smart back in the middle of the world and then we’re going to use that to place the player so when we generate world will generate the world first and then we will place the player don’t position equals spawn Location keep doing that maybe we should just rename it to spawn location spawn position so right now we still have this function this generate in the world is doing the full world sizing chunks which we don’t want it to do without that we’ve changes a world size it’s quite Large now we only want to do the view distance so our view distance is what a reset it at five our view distance is five so that means that there should never be a square of more than ten chunks five in any direction away from the Player so we will start first off we need to start we need to get a center point which we know is voxel data dot world sizing chunks divided not compared two divided by two so that gets is the middle of the of the world in so I can’t Put a flock there the middle that gets us to the middle of the world in chunks and then we need to might from that we need to minus Vauxhall data dot view distance in chunks so that gets us that should get let’s say the center is 50 And then obviously the view distance is five so that should get us to 45 so we start in x equals 45 with the current values that we have and then we can just copy this for the value that we’re falling to and we just change this tuple Because now we go in from the center plus the view distance and then we can copy all of this for here because again it is exactly the same and I’ve just I’ve just realized this is actually I’m sorry about this again making mistakes this needs to be times by the chunk Width otherwise it’s going to be a drastically wrong value just copy that wholesale if we try that we should have Oh No the camera is still drastically wrong Oh yep sorry hmm it will help you for you set the spawn value before using the spawn value So yeah that could be we’re gonna have to raise the height of the camera because right now it’s appearing pretty much on exact level with the ground so you can’t see anything but as you can see the camera has been moved to the dead center of where the chunks are Spawning now the world extends out another 40 blocks in either direction and sorry another 20 blocks in the other direction we can’t see that now because he’s only drawing the view distance but let’s just quickly change this give this a little bit more height so at least now We should be able to see it yeah there we go right so what we need to do now this is the the slightly tricky bit is we need to update that during game time because there’s no point how about a view distance if it only works you know When you’re generating the world view distance is for runtime so let us create another function and we’re going to call this one check view distance and it will take in what I want to take in I don’t think I know we didn’t take anything on this one so we need to get The players position as a chunk so whichever like as in wherever the players in the world we need to figure out what chunk that player that position puts the player in and to do that we’re going to create analytical function just to make that easier because we was going To use this again in other places so we’re just gonna call this get chunk from get chunk card sorry from vector3 and it will take in a vector3 only enough nice and simple function all it’s going to do is create an X in s 0 you Have floor to int we’re passing in the pause X and dividing it by chunk width and then we want to do exactly the same for Zed and then we will return a new chunk card using those values now we can use that to create a new chunk card I’ll just Call it card like we usually do and it will equal get chunk card from vector three and we’re going to plus it pass in the player dot position value I’ve already got card in this no you just don’t pour new that right so we then loop through and x equals card X minus Voxel data dot view distance in chunks and then X wants to be less than card X plus voxel data view distance in chunks X plus plus and then we shall just copy and paste this for a z-value and what this is doing is basically doing the exact same Loop that our generate world thing is doing only instead of working from the dead center of the world of a spawn position it is working from wherever the player is currently standing so within this loop first thing we want to do is check that this chunk is in the world Card yes or just passing god if chunk is in world then we do the stuff that we’re about to do so the first thing we want to do is if chunks card X card Z is equal to null then we want to create this chunk because it means that is Within the world it’s not outside the boundaries of the world so we do want there to be a chunk there if it’s been if we got to this point in the code then it’s within view distance of the player and if this equals null that means it’s Not been made before the the actual thing the chunk has not being generated before so we want to generate it else oh yeah nope sorry this that’s all we need for now um so if we run there now so if we run that it will make absolutely no Difference because we haven’t got an update check real distance sorry these they should actually be the for loop values probably obvious to you as you were watching it but as I’ve said I’m not 100% full lucid right now so if we try that now you should have a rudimentary view distance system okay As you can see generates them as you move around and theoretically if I run right to the edge of where the world ends we get out of range exception oh yeah the sorry about this the error is because we are not using the x and z values here again can only apologize so Now we are generating chunks the world over and it will stop when we get to sorry I’ll just take that angle off the camera and it will stop when we reached the edge of the world now you may notice the problem right now is that the view Distance isn’t much used to us when it is leaving behind all the chunks that we generate we don’t want it to leave them behind once they’ve been generated so we need a way of knowing which trunks which chunks are active and on deactivating those chunks once they are out of view Distance so the way we’re going to do that is we need a list and I said we were going to use lists for storing everything because it’s not very efficient but this will be a relatively small list and we’re going to call this we’re going to use chunk cards because We literally the only information we need is the x and z value everything else we can work out from that in the same way that we look up our block type from the byte number we can look up the chunk from the x and z value and we’ll Just call it all right nice and simple active chunks equals new list and then what we’re going to do is every time we create a new chunk because we’re not going to create any chunks that aren’t active we don’t create chunks off in the distance that aren’t been seen we Want to create a chunk when it’s gonna be on screen so anytime we create trunk we will add we will add it to the active chunks list chunks X chunk card X so now that list will keep all the active chunks obviously we now need a way of Removing chunks from that so if we go back to our chat view distance script we’re going to go straight to if statement where we check if the chunk exists in our chunk map already and we’re gonna have an else on there and that else will be to check if our chunk Is active so this is if the chunk exists we need to check if it’s active so chunks X Z dot is active if you remember this is pulling from our little function we made just to make life easier on herself that I have lost there is that’s Pulling from this all this doing is checking the game object itself so if it is active sorry if it’s not active then we want to make it active we also want to spell else with an owl and not okay so Chun if it’s not active make it active Equals true and then we want to add it to the active chunks right but we now need to be able to disable the ones that are out and we’re just going to do that by setting them to inactive but we need to know which ones are inactive all need To be made inactive so what we’re going to do the way we’re going to do this is we’re going to create a temporary list here and we’ll call this list previously active chunks so chunk card previously active chunks equals new list and we’re going to copy in the active chunks list So whatever chunks are active at the time we run this script here sorry that’s incorrect syntax chunk card active chunks so whatever chunks are active at the time we run this function will be added into this list within this if statement here where we’re checking if the chunk is in The world so whatever is happening here is with a chunk that is a valid one that we can work on within the actual world itself we’re going to do a quick loop through our previously active chunks I equals not I is less than previously active chunks dot count i plus plus and We’re going to need to do a little so what we want to do here is for each item in this list we want to compare it to the current chunk so if the one of the items in the previously active chunks is this chunk the one that the one that We’re currently looking at which we know is within the view distance otherwise we wouldn’t be looking at it and we know it’s within the world otherwise again we wouldn’t be looking at it so we know is a chunk that we want to be active and on screen so we need to Compare it against the previously active chunks and to do that we’re gonna create a little function inside here to just compare this chunk card against another chunk car that we passed to and we’ll do that with a public bull and we’ll just call it equals it will take in one trump Card which is the other john card the one that has been compared to if other for some reason equals no we’re just gonna return false else if other X is equal to X and other dot Z is equal to Z return true else return false so all This does is we can pass a Chon card into here and if X and z are the same it will return true it’s just a simple this strong card is the same as this junk odd function so inside our four statement here we can now do a bit of a check and What we want to do is if previously active chunks I dot equals the currently the one we’re working on which is new chunk called XC then we want to remove it and this is why we used a for a proper file loop instead of a for each loop because we Needed to be able to access the indexes that we’re currently working on remove at I so what that’s going to do is we start this function with a list that contains all the chunks that are currently active on screen we then do a check to see which chunks are currently Within the view distance any chunks that are within the view distance will then be removed from that list so once we have finished we know that any chunks left in that view that within this list here are ones that were previously in the view distance but I’m no longer in The view distance so then at the end of the function I’ve got the right curly bracket here so then at the end of the function we just do a quick and now we can use a for each we just do a quick loop through chunk card we’ll just call It C it really doesn’t matter in previously active chunks we use that CX C dot Zed is damn sorry dart is active equals false and we just make it inactive because we don’t want to remove the chunk completely because then if we ever wander back into that area we’re Gonna have to generate it all over again and that’s very inefficient when you get to a certain size you will have to worry about storing too much stuff in the memory but for now that’s not a problem for us if we run it we should only see chunks Within the view distance yep so if you notice it is following us essentially now you can’t really tell I I can barely tell looking at my screen and I imagine once it’s there any frames lost through the screen capture and being uploaded to YouTube it might be Even more difficult to tell but there was a bit of a hit on the framerate and that is because it is doing this every frame regardless of whether it needs to and it won’t always need to we only need to do that if the player has moved from The chunk that they got moved from one chunk to another so we need to keep track of that and the nice and simple way of doing that is we just have ourselves a chunk called value that we will call player last chunk card and all That means is it was the the last chunk that the player was known to be on and so we will put when we start we want to generate it from the beginning so we will put player last chunk card which is also the first chunk card but obviously After we’ve generated the spawn position we will use our get chunk card from back to three function passing our player dot position so now I’ll play a last chunk cod is set to start with which will be current values that we haven’t it will Be 50 B 50 or whatever is and then in here we’re going to do a quick check and it’s if get Chunk card from vector3 player yet play it up position dot equals play a last jump card then we want to check the view distance so we’re only checking the View distance now if the player has changed and then obviously we need to make sure we update that player last known in fact you know what we will star a value for it so we’ll just do this in the update loop so we’ve got our player last rung card We’ll just have chunk card player chunk card and then we’ll set that in our update loop equals get chunk card from vector three player dot position and then we don’t need this anymore we can just use our player chunk card if playin card equals player lustrum Sorry if play trimco does not equal they play a lustrum card then do the view distance check if it does equal it means the play is in the same chunk and we don’t need to check the view distance again because we know the player hasn’t moved or hasn’t moved far enough Obviously the chunk is however many blocks wide fifteen or sixteen so it means that in this case the player will move fifteen sixteen blocks without the view distance updating but that’s a judgment call maybe you wanted to update every single block personally I feel that’s way over overkill but that’s Entirely up to you so it won’t look a trial or different to the last time but as you can see it’s everything’s still working the same it’s just a running a tad more smoothly now because it’s not updating every frame it’s only updating when he actually needs to and if you Disappear off the edge of the world it disappears with you and that is the end of this tutorial I’m sorry it was a bit of a slog and there’s an interruption in the middle hopefully everything’s clear if not the code will be available as always you can look Through yourself I’ve tried to keep things as simple as possible I might go through and out a few annotations in the code before I upload it so I make things a bit easier for you but in the next tutorial we’re going to get to actually making this look like Actual terrain as opposed to just a flat grass plane so hopefully that’s one for you to look forward to and I will see you next time Video Information
This video, titled ‘Make Minecraft in Unity 3D Tutorial – 04 – Chunk Generation’, was uploaded by b3agz on 2019-02-26 09:27:41. It has garnered 67077 views and 1377 likes. The duration of the video is 01:04:32 or 3872 seconds.
JOIN THE DISCORD SERVER!
———————————————————————————–
In this episode we create our chunk generation system, allowing the game to dynamically create new chunks on the fly depending on where the “player” is in the world.
This video was a bit of a slog due me struggling with a pretty aggressive cold, but I think I’ve edited out all the coughing and spluttering. So if you notice a bit where it looks like something was cut, it was probably because I had a coughing fit :-/
You can find the assets for this episode here;
https://github.com/b3agz/Code-A-Game-Like-Minecraft-In-Unity/tree/master/04-chunk-generation/Assets
SOCIAL STUFF!
Website: http://b3agz.com Twitter: http://twitter.com/b3agz Instagram: http://instragram.com/b3agz
———————————————————————————–
SUPPORT THE CHANNEL
The easiest way to support the channel is by liking, subscribing, and sharing. But if you want to go that little bit further, sign up for my Patreon at; https://www.patreon.com/b3agz