In the previous video we have implemented our digging mechanic and we have uncovered that there is one issue that our chunk is not updating the neighbors when we remove from it the edge block so we are going to fix this issue in this video okay so to do this we are Going to open up our world script again okay and we need to go back to our set method that we have implemented you can use this drop drop-down list to find your setup method and beside updating this chunk we will need to add some code here To find out if our block is on the edge of our chunk and if it is we will want to uncover which other chunks should we update after removing this specific block okay so let me paste the code so first thing as i have said we need to find out If our block is on the edge of our chunk so we are going to pass the chunk is on edge to the if statement and this is on edge method we’ll take chunk dot chunk data so this is the data behind our chunk and our position that we have calculated What is the position of the specific block so if we get this if statement we are going to perform some other logic but let’s start by implementing this method so let’s right click quick actions and generate this method inside our chunk static class right click on this and go to the definition okay So this will be inside our chunk static class at the bottom i think and in here we are going to create logic such as vector3 int new position equals get block in chunk coordinates so we need to get the block in chunk coordinates that this method was implemented again in our chunk And it simply converts the world position to chunk position so we are going to get the values from 0 to 15 or to chunk size and next we need to create this if statement so if newposition.x is equal to 0 or to the chunk size minus 1 This of course means that this is a on the edge so if either x y or z is equal to 0 or our x y or z is equal to chunk size minus one of course y would be checking chunk height minus one because this chunk height is a bit different So in case any of those conditions is met we are going to return true else we are going to return false now we could of course call this right click and rename and let’s call it a world position since this makes a difference for us if this is a world position And we can rename this one to be local position or chunk position since this is pretty important okay so if this is done let’s go back to our world script and we have received our response if this block is on the edge or not If it is we need to get a list of neighbor chunks to do this we are going to create a neighbor data list so let’s maybe type list of chunk data this will be easier for our visual studio to create this method neighbors data list equals getchunk.getedge Neighborchunk and we are going to pass here chunk data and the position again right click on this method and generate it using the quick action menu and right click on this and go to the definition okay let me paste the code here we are going to have a pretty long if Check so we are going to pass here a vector 3 in new position which again this will be the world position and this will be the local position we can simply right click rename it and let’s call it world position and this will be vector 3 in New position so let’s call it maybe local or chunk position this will be equal to get block in chunk coordinates check data and the world position if we have this we will need to have a list of chunk data neighbors to update equals a new list of chunk data Now below there is a set of if checks so basically we need to check if our position.x is equal to zero so if on x we are at the position zero we need to get a chunk to the left from our chunk if this is 15 or the chunk size minus one We need to get the positive chunk to the right and this will be again for y it will be up or down chunk and for z it will be chunk forward or chunk backwards from our current position so this is how we are going to access the correct neighbor So again we will need to have a set of if else statements if xx equal to zero then we are going to add our neighbors to update add world data helper and we have this git chunk data method but we haven’t checked or changed its protection level so if right Click on this and go to the definition we will see that this is a private method so i will make it a into a public method okay and this is our method inside our world data helper and this simply gets the chunk data from a position that will pass to it And let’s go back to the chunk script and here we are going to get the neighbors to update odd world datahelper.getchunkeddata we are going to pass here the chunk data the argument to have passed to this method dot world reference and we are going to have the world’s position Minus the vector3 in the dot right and this is simply vectors one zero zero so if we want to get the chunk to the left because x is equal to zero so we need to get the chunk to the left so if it is so we need to subtract one From our current position to get the block inside the chunk to the left okay so you can simply copy this statement and instead of zero paste here chunk data.chunk size minus one so this is the last block in our chunk in the x-axis we’re going to add our neighbors to update Simply world position plus the vector3 int dot right so we are going to add one to it to get the block in that chunk to the right again we can copy those statements and do the same for y axis so for y axis equals 0 the only Difference will be that will be world position minus vector 3 into dot up so this is 0 1 0 and for the y equals chunk data.chunk height minus one we are going to simply add to this world position one on y axis and the same will be on the z values so For the z value z equals zero we are going to use vector3 int dot forward which is 0 0 1 vector and if z is 0 we are going to subtract the one from our current position of the block for z equals to chunk data.chunk size -1 We are going to add a vector 3 in the dot forward so add 1 on the z-axis so that we can get the correct chunk now we cannot really add the else statement because our block can be on on the edge of our chunk so this would mean that we will get the Position to the right to the left for example and downwards and this will mean that people need to couple of those conditions will be checked so this is why we need to have all those if statements instead of if else statements so at the end we are going to simply return Our list of chunk data to get the neighbors that we need to update let’s file save all and let’s go back to our world script okay so we have this list so we can look for each var neighbor so this would be the chunk data neighbor data in our neighbor data list For each of those now the first line would not need this actually because we want to only set the modified by the player to the check that was actually modified and the rest of those of them the neighbors will be rendered correctly because the data will be In the dictionary when we render those additional chunks now the first line now we in this for each statement will be chunk rendered chunk to update we are going to access it by calling world data helper get chunk now we want to access this chunk renderer So to get chunk we are going to pass neighbor data the twirled reference and the neighbordata.worldposition so let’s right click on this method quick actions and generate it okay and at the end if the chunk to update is not equal to null we are going to call to the update on this chunk So we are going to update all the neighbors and at the end we are going to have the remaining code so chunk update chunk and return true so we have updated all the chunks so last thing to do we need to implement this git chunk method So let’s right click on this and go to the definition okay so here we are going to have this simple logic we’re going to check if our world reference dot world data dot chunk dictionary so this is the dictionary of chunk renders contains this key so this will be our world position We are going to return our world reference world data chunk dictionary and this chunk renderer on this position else we are going to return null so let’s make sure that we file and save all scripts and let’s go to the unity so now let’s press play and let’s see When we generate our world okay i think that here we are going to reach the end of our chunks and let’s control shift p to pause our game and let’s see where we are now so this was the current chunk let’s click f to focus on it so our camera will not Clip it let’s see here was the last chunk and this was the remaining chunk so that the neighbor so here we had this line crossing and this block if we take a look at it from the bottom it has no face here generated okay this doesn’t look terribly helpful So now if we unpause our game and remove this block as you can see this and it was blocked from our chunk that was a neighbor and both of those has faces here so now what we could do if also is also to check if for example here we are going to Update both of those chunks so let’s go to our corner of a chunk somewhere okay and i think this is here so as you can see we now have affected this chunk this chunk and i think we need to dig one block down and now we have updated this chunk i Think this one and inside this one and inside this one so basically we are should be able to dig around here and it all should be updated correctly we should not see an empty space anywhere now of course it will not work for our water because we Have no logic for the water to flow through those holes but basically now we have our digging mechanic complete last thing that i want to tackle here is the generation of our chunks that are underground in our world we had this method called get positions that player sees Let’s right click on this this was in the generate world let’s right click on this and go to the definition and in here we have received those chunk positions around the player using the world data helper so i want to go to one of those methods And here we have in the get chunk positions around the player we had this if statement to get the positions underground and i have told you that we are going to wait with this code until we implement the digging mechanic so let’s uncomment this code ctrl k ctrl u to uncomment it And we have those checks here so this was for the git chunk positions around the player without one added to the range now below or above here we should have the second method here it is now here we want to again uncomment this code and we are not going to play around with Adding one to this let’s now test let’s for now test if this all works let’s save this class and if we go back to unity okay if we try pressing play now and regenerating our world we’re going to get an error now exception is need to ask world for appropriate chunk So this is the error that we have thrown inside our set block in our chunk static class and this is something that we have forgotten to implement or actually i have forgotten to implement because it was not needed until now so what we are going to call here is our World datahelper.setblock and we are going to pass the chunkdata.worldreference localposition and our blocktype so as you might recall i think in the previous video we have implemented this method to set the block type for the specific position okay so let’s make sure that we file and save All and let’s go back to unity okay so if we press play now we’re going to see that our chunks were rendered pretty much correctly and our lower chunks were also rendered for some reason using our setup that is incorrect so we did not want to render anything here all of those should Be said to be of type of block there should be no error and of course we can see that we have loaded those chunks so we have this data but for some reason we have also used pearly noise value on it okay so how do we find out what is Wrong well i’m going to go to our world script great now in our world generate world method that takes the parameter we are looping for each of those chunks we remove them and at some point we are generating the chunk data so let’s go to our for loop that loops For each position in our chunk data positions to create and let’s add a break point here at line 59 now i’m going to exit the visual studio full screen mode and i’m going to simply attach this line to unity and now if we go back to Unity okay make sure that you have this checkbox debugger attached to be blue i think you can you need to click it to enable it you can now press play and when we click on regenerate our visual studio should be shown or you can click it in your Taskbar so we can click this um i think step into so f11 and we will go into our generate chunk data now this is all great we are entering this for loop so let’s click again this f11 to go into this for loop and now we are processing the position x and z Which is zero and zero if we again click on step into we are going to go into process chunk inside our biome generator and here will be our issue let’s step over so f10 and here what is going on here we are calling start layer handler.handle And we are passing y equals zero to until y equals is less than data.chunk height but our chunks beneath our terrain has the y equals to minus 100 or the minus chunk height so here is the culprit this for loop is passing wrong data on the y-coordinates to our Layers so let’s stop debugging by pressing the square we are going to stop our game from executing the code and we are going to go back to our visual studio okay so inside our biome generator we have this bug inside our process chunk column we are Using this for loop and in the for loop we are going to instead of calling zero we are going to call data dot worm position dot y let’s copy this because we are going to simply loop until y is less than theta that worldposition.y plus data the chunk height Now in this instance for example if we go to our air layer you can see that we are taking in the x y and z positions and we are checking the y position against the surface height noise and as you might recall this is some value like eight So if y which will in our case for the underground chunks will be minus 100 is minus 100 is greater than 8 not at all so if we go back to our umt we are going to see that in our terrain generator our biome generator uses the water layer then air layer Surface layer and at the end we are entering the underground layer which is the last layer so if we edit it it is checking if y is less than service height noise and that’s exactly what is going on in our case we are going to set the Chunk block to be this block type and in our case it is dirt so now if we save it if we press play we should see that if we regenerate our map we are going to have the correct um or rather we are not going to basically see anything But we have a way to visualize our chunks if we select all the chunks now of course you could have provided a better way to store those chunks in one parent object in any case let’s select all of those chunks in our chunk renderer script we should have this show gizmo and as You can see we are rendering all those gizmos and our gizmos takes six chunks at the bottom so this is the second level this is the first level of the underground terrain and those are our chunks at the top so we have rendered correctly our chunks or rather we have those chunks rendered And all of those are said to be dirt we do not render anything we can only show this gizmo so we can disable those gizmos and now uh let’s select this chunk render script and disable those and in our game view we should be able to now dig Into the ground without any issue because we have those chunks loaded correctly underground as you can see okay let’s select this tunnel and as you can see this is our tunnel is it rendered only the faces inside the tunnel but basically we can dig forward of course our camera Is a bit misplaced here it is clipping through our underground terrain but basically we can delete those chunks here uh those blocks and you can see that we can dig underground we can look up and here is our exit again we can press v and spacebar and of course now we have this This underground digging possible and as you can see it here is our tunnel that we have generated in the underground chunk now we are not going to go any further with this system of course when you are underground you could have unloaded all the remaining chunks Around it you could focus on only those chunks below the surface of the train now this is something more advanced that you might want to tackle to make this uh world generator this voxel world generator more efficient and i think that it is finally time to implement our multi-threading because when we are Moving this is driving me crazy when we are we need to load uh this weight and freeze to wait for all the chunks to be loaded so i want to mitigate it and in the next video we are going to start working on the multi-threading so that our world Will be loaded on the fly we will not have this those severe fps drops okay if you’re enjoying this tutorial please leave a like leave a comment subscribe to the channel became a patron or youtube member it would help me a lot and if you want to explore how to create A 2d platformer i’ll learn a bit more how to structure your code pace a bit better how to use design patterns in your game code check out my 2d platformer course ok see in the next video Video Information
This video, titled ‘Create MINECRAFT in Unity – S2 – P17 Digging mechanic P2’, was uploaded by Sunny Valley Studio on 2021-07-27 21:57:19. It has garnered 2257 views and 80 likes. The duration of the video is 00:21:10 or 1270 seconds.
In the Section 2 of this procedural voxel world generation Unity 2020 tutorial we will explore different procedural algorithms which uses Perlin noise to generate a nice looking landscape for our infinite voxel world.
Section 1 – Voxel Terrain Engine https://youtube.com/playlist?list=PLcRSafycjWFdYej0h_9sMD6rEUCpa7hDH
Section 2 – Procedural generation https://youtube.com/playlist?list=PLcRSafycjWFesScBq3JgHMNd9Tidvk9hE
Section 3 – Multithreading
Learn more from about Unity Game Dev in my video courses at: https://courses.sunnyvalleystudio.com/
Code files for everyone: https://www.subscribepage.com/voxel_world_tutorial_section_2
Project / code files for patreons: https://www.patreon.com/posts/voxel-world-end-53323870?utm_medium=clipboard_copy&utm_source=copy_to_clipboard&utm_campaign=postshare
Project / code files for YT members: Link in members only community post: https://www.youtube.com/c/SunnyValleyStudio/community
You can support me through Patreon: https://www.patreon.com/sunnyvalleystudio
Articles: Perlin noise – https://adrianb.io/2014/08/09/perlinnoise.html Domain Warping – http://www.nolithius.com/articles/world-generation/world-generation-techniques-domain-warping
Join the discord if you have any questions! https://discord.gg/RQEtYHz