Hello and welcome to this unity tutorial video we’re just just landed oh yeah we’re especially for King Camille who commented in a previous video wanted to know more about using a seed to generate a terrain such as this so what I’m gonna do in this video there’s about three Things we try remember now in a in the previous video which was kind of not a tutorial but coding like a speed coding challenge I created these trees they look like trees yet so perhaps we could make those look more like trees and in Camille you asked about all the extra Code that I put into my into my project which I didn’t really explain so I could do that and all that is is making sure that for let’s see if I can show you for each of the different tiles that I’ve got in my scene so here I’ve got I’ve Actually got not just one terrain but two squares and they each generate their terrain separately but using the same using the same what was it called a simple voxel farm script as we looked at in previous videos but if you say wanted to randomize seeds and each script or Each voxel farmers doing that separately then the terrain won’t match up at all and so you’ve got to have a way to to organize and coordinate your different like terrain tiles according to a one seed so we’ll do that and that will involve having game objects talk to one Another so let’s set up this let’s set up this project so I’ve made a new folder in my project and I’ve made a new scene I’ve called it Camille seed scene so what I’m gonna do is delete what I’ve got so far well better try and delete it there we Go delete what I’ve got so far so that all we’ve got in the scene is and I’ll also delete the script I made there we go so we’ve got in the scene is a player that can move around first-person controller the sky sorry the Sun and a voxel farm which is just a Where is it it’s just a an empty game object to which I’ve got attached the seeding farm script okay so this seeding farm script is derived from the script I made in previous videos but simplified and I’m gonna simplify it even more now so in the takeaway anything to do with Seed so let’s take away this and we want to take away the seed from here so I’ll do everything from scratch there we go okay so it’s a short script it’s less than a hundred lines and all this does is create a grid in generate terrain a Grid of one hundred by hundred voxels I’ve just got a nested loop and then using Perlin noise we decides a Y position of each block so 100 times 100 and then we create a new block and we go and grab a prefab actually we grab Either a grass block there we go or we grab a soil block depending on the height and that we’ve decided with the Perlin noise for each block okay I’ve just reminded myself you might have seen that actually still in the preview thumbnail down here these blocks are really tall and another comment I Apologize a confident remember who who asked but we created this dig Li script in a previous minecraft tutorial and that allows allowed us to create blocks in game and also to dig and of course we’ve just got a thin layer of voxels you know that build a Terrain and of course if we dig down then we’ve got nothing underneath so the question is asked well how do we do multiple layers and really really the method that we’re using where we’re just creating blocks is going to be far too slow to create a massive Minecraft world With lots of layers but we could think of I was going to say clever they’re not really clever which is stupid little hacks to inventively kind of get around this problem so what I’ve done is just I’ve only done one thing so far but you could make each block like ten high in Terms of its scale in for its y-component and then when you dig down it will give the illusion that you’ve got this really thick terrain so that’s one way I think I may have broken my script so I’d like to go and try that out things are not the right color here What am I missing not sure anyway let’s go back to the folder so we were in seeding nice to try and run it see what happens should just like an ordinary terrain with trip okay good bye ruff we’ve got to get onto the terrain voxel Farm I just put you at zero there we go brilliant okay so very very basically we’re generating a terrain and an trees or the beginnings of trees now let’s go show you this thing about digging around so now if I dig so if I press the right mouse button There we go I believe all its really doing is moving the block which remember is a really tall stalk down and so it gives the illusion that you can dig and it’s you know you’re gonna run into problems when you’re at the side of a Wall like this and you try and dig and instead of a hole being produced you’ll just move the whole stack down I’ll try to show you in a minute but we can also gig there we go the problem being here that it might create a massive stalk because we’re We’re not just creating a block we’re and instantiating or loading in a prefab but I don’t know it’s a star that’s not bad there we go I was godlike so if I face a wall like this it doesn’t seem to do anything but actually it’s made it Shorter if I keep pressing it will go down and down and down okay so that’s some for whoever it was I apologize who commented in the or after in the comments how might we add layers thus if you’re sticking to very basic game object cubes you’re not really Optimizing anything like this but you just want you know just want to play around then that’s one way you could have layers in a fairly efficient way it wouldn’t be very efficient to say create this terrain then clone it and then stack loads of them up but you can Achieve a very similar thing by having an elongated voxel so just to show you the kind of the consequences of that if we return these Y components of the scale to one and play this again the amplitude of frequency of our Perlin noise terrain will stretch the voxel too far From one another and you’ll have these horrible gaps inside which you may have found if you’ve tried out my code and changed that amplitude and frequency so another benefit of having longer voxels is that you hide behind these gaps and you create a continuous seamless terrain And you can have them as large as you like so let’s go crazy a hundred a hundred brilliant right ah So one thing I said was I wanted to fix those trees the main thing is to coordinate a seed for if we have different voxel farms so I’m going to get two but I’m gonna make another Rock for farm now and I call this one we’ll call this one Camille Camille farm in Honor of king Camille and this needs to be at least a hundred away or put you 101 and then it’s going to see if that’s now in-game if you go on an adventure and go and find this other voxel farm it should be over here and there should be A gap between them and so now that we’ve got two we can add a seed and see what happens if the seeds are different oh and what I was going to say was in terms of amplitude of frequency it would be much more interesting to have the seed Impact the amplitude of frequency so that the seed number was actually involved in the algorithm for generating the terrain and not simply finding a new location and kin in the virtual purling terrain and so giving the illusion of a new terrain kind of generated from that Seed number that will make sense if you look at a couple of videos ago that I made when I first made the seed number there isn’t a gap let’s pause this Camille farm if i zoom out okay I want to look down on the Y dimension okay we’ve definitely got to Vox with Farms oh there is a gap there’s definitely a gap but I’ve missed it I somehow jumped over it where am i me I went down there I’m only in the middle of the terrain I saw that was on the edge I definitely went over the cap then did you spot the gap Otherwise I was walking was I talking and then I missed it there was I did get distracted actually there was a there was I think it was like this little section that reminded me of Superman’s home in the ice with the crystals and night I was looking at that Oh was that The gap I’m not by the gap I’ve got to be over halfway now I mean I’m reciting this serves no purpose yeah admit the gap is undetectable there you go if you want to have a gap in the middle of your terrain that seems to be fine it won’t upset Anyone let’s make a larger gap come on farm 101 well we’ll make you 110 that should be a significant gap in there in the terrain let’s go find that now I can see it surely I can see it there this is a very interesting part of the Perlin Noise terrain it’s like a really straight ledge quite like it along this ridge which is kind of it’s you know it’s is interesting because it’s it’s generated from pseudo randomness so it’s genuinely like a there are things to discover in the landscape in terms of its features and here is almost like a Grid that’s really cool anyway seed number zero okay that’s kind of cool as well now I’ve got the sheer drop have I just fallen down yeah there we go so we definitely got we’ve definitely got a GAF now so we can see our two voxel farms right let’s get going We haven’t done any coding yet so we want to add a seed component to our code here right so we want a public C public sorry it’s going to be an integer you could make it float that’d be fine or work with the algorithm and the default Value will say is zero okay so where does the seed go I don’t really need to talk too much about this because I covered it in a previous video but it will play a role in where we’re we’re looking in terms of the two-dimensional Perlin noise so I’m going to add it to Just the X component this Perlin noise function by the way it takes in an X and under Y or thinking about the three-dimensional space X and Z and then that will return a number between 0 and 1 and then I need to scale that up by the amplitude and that means kind of Like the volume or the in terms of like a sound wave or just how big the waves are going to be or Hills are going to be for our terrain I’m just I’m getting distracted by the FATA the the script should not be looking that color I don’t Know what’s going on it’s because I’ve like broken it but it’s still working anyway right we’ve got a seed so our Camille farm and our voxel farm have now got a space for their seeds so you can have radically different seeds so we have one what a big change from zero one four Voxel farm and then let’s have $9.99 $9.99 for Camille and then when we run they will have they will be starting from different positions in the purlins terrain well interestingly still got like a bridge oh yeah of course because the first box will farm you change from Zero to one so its landscape is gonna be almost identical now now notice that if I go up on this high crest if we if we have positioned Camille voxel farm where it should be these terrains would not line up and so you get a complete mismatch although It’d be very hard to see because because I’ve got these really long voxels it will kind of disguise the fact that there is a seam here and indeed even when there was a single gap I found it difficult to see it so it’s important to have the same seed unless you want to Create variety that that will include a seam between your voxels voxel farms so what we’ve really got to have it is like a third party we’ve got to have another empty game object that will be kind of our seed manager there we go so we’ve Got a seed manager in there which at the moment we can just put it in the middle of the of the world it doesn’t matter where it is doesn’t matter what scale we’re not gonna be able to see it you could attach a model to it but it’ll be purely Aesthetic it wouldn’t do anything but we need to write a script that will control what seed each of our farms are going to have and if you were creating like an infinite terrain where you’re kind of like moving these farms around as as a player moved to give the illusion of an Vennett terrain I’d love to do I must work out how to do it in a good way and do that in the next video or something then you would want the seed to be consistent I also now want to make sure that the seed impacts upon how the Terrain looks so it will change the frequency and the amplitude so first let’s just make sure that our seeds are always the same so we need a new script make this in c-sharp I’m gonna call this the seed brain there we go okay and this Is gonna be quite simple all we want to do is reproduce the feature of our seeding farm of having a seed number public my colors are back again it didn’t like the fat my project didn’t like the fact that I deleted that script oh that’s lovely So pleased it looks nice again I hope this font size is OK for you let me know if not so in generate terrain before this seed is used we want to make sure that see brains told us what seed to use so we won’t need starter update in our C Brain we just need a public public int seed there we go and again will have the default value set to set to zero so have I attached this script to the seed manager no note so I need to do that right so seed brain attach there and we Can see the public variable 0 so we can change the change the seed in the inspector which is nice but this of course has nothing to do yet with our voxel farms voxel farm and Camille Val okay so we really want to decide who’s going to talk to who is the seed manager Going to talk to each individual Jaffar was each individual voxel farm going to talk to you know start the conversation with the seed manager and this is I heard it somewhere in some kind of video about programming this is really the essence of object orientated programming so not not really that You’ve got objects of each separate script on although that’s kind of essential in a way but the fact that those different objects can communicate to one another and will control their own data that’s really the the essence of object orientated programming so I think it makes sense for when each voxel Farm wants to generate its terrain it knows that it needs a seed so it’s kind of makes sense for it to go and ask the seed manager for the for the seed that all the other voxel farms are using so generate terrain maybe we should write a Function called get my seed there we go and we’ve got a right that can be nice I I haven’t written enough functions with return values so let’s say it returns an integer get my does it return something it kind of reflects the name of the function a little better so we’ll say Seed equals get my seed and so this function wants to return a seed that we get from the seed manager so how do you talk to another object so one way you can do that is by using the game object dot find function and then you can put In here at the name of of the the game object you want to find so I think I called it seed manager I’ve got to be careful there because this string has to match this string seed manager exactly so a lowercase uppercase C manager all together no spaces that’s exactly the same So I need to obviously store whatever this function finds in the appropriate variable so the appropriate variable will be a game object so game object for game object and we’ll call it seed manager equals that I’ve just called I’ve used an underscore so I know it’s Kind of separate in my mind from the actual seed manager object out here so it’s not the game object is just the Verrill we’re storing that game object in okay so we’ve we’ve now kind of got hold of another game object and what we’re really after is this script the Seed brain and in particular specifically the seed number that that script is holding here it is in the inspector so there’s a seed bearing print script and it’s got that seed on it so we need to get hold of that now we can do that now that we’ve got hold of Our seed manager so we can say return just delete this whitespace that’s an error here I’ll probably because we haven’t returned anything yet so return seed manager dot no yes get to component so we’re grabbing a component and the component we want to grab is the seed Brain and if it’s a script that will come up in in the mono developer list which is nice so seed brain brackets dot and now we can go and get the seed there we go brilliant and all the colors have done the correct color so that will now Get my seed will return the seed that’s on the seed manager now let’s just see if this has worked the seat is zero on the seed manager although no it isn’t it’s 999 on voxel farm presently the seed is one and I’m Camille it’s nine and a nine Right so if we run both of those scripts that controlled generating the terrain before they generate they should go and get the seed from the same place so there should be both set to 999 which I could just go and very easily check now by pausing the game and looking at their Seed numbers but I’d rather first just go and run to the edge the gap and just see by eye if it seems to line up it already looks like that way I’ve got this nice feature please don’t fall off there we go so the terrain is looking Like it lines up exactly there we go so it looks like the seed numbers are going to line up okay let’s go check that it could just be illusion it could just be an illusion right so voxel farm see number 999 Camille farm 999 and that’s because they’re both derived from Both derive from the seed on our seed brain which is 909 okay so good so far so that’s worked so that’s how you might coordinate your seed numbers so the next thing we need to do is use the seed in a more interesting way we could have a Let’s have a randomizing function on the seed brain or by the way if I made this variable private and water the point didn’t make it public then if we try and run now should run into an error yes all compile errors have to be fixed before We can enter play night so keep in mind that does appear there that’s interesting I didn’t know that was going to appear in the see brain script because it’s private maybe because it’s up here it’s not within a function I didn’t realize I thought every time you made a an integer public That’s what caused it to be seen in the in the inspector unless that hasn’t changed back because there’s a I might not have altered because there’s a there’s a fault on the other there’s a bug on the other script if I just comment that out that comment all that Out now will that disappear from the inspector yes it does okay I’m not going crazy right meaning yes if this is private it won’t appear in the inspector but just as you as you just saw it means other scripts can’t access it which is a way you would control control cannot the Complexity of what’s happening with your data and information so that it’s easier to debug later on you know I you know which individual script is affecting what data and stuff so while I’m doing actually is quite bad I’m allowing scripts to kind of well not allow I’m Not changing the the seed number some okay but if you allow different scripts to change the seed number for instance and there was a problem a bug it could involve any one of those different game objects and so you’ll be in trouble to try and debug it okay right so we want That definitely we want that public and I want to uncomment those and I’m coming down and so we want to make this seed number once we’ve grabbed it affect our frequency and amplitude I haven’t really done this before so all you really essential thing is the seed number is kind of has certain Characteristics it’s kind of like mathematics which I don’t know anything about but you can ask of that number certain questions like are you or are you a multiple of 94 do you like poached eggs can you fit within a certain area if we doubled you up or something I don’t know You can ask different kind of like mathematical questions about it and depending on those different the different answers you get you could then change the features of the terrain and so in that way the seed number has meaning in terms of what it will manifest for your voxel terrain so I Think that’s quite quite an interesting thing to do so let’s have a go it’s my first time at this so what we really want to do this effect the frequency and the amplitude and we can play around with these quite quite freely because our voxel stalks are really elongated so If we stretch the terrain out where there would ordinarily be massive gaps now we can play around with it and those gaps won’t be too but there won’t be any gaps unless we stretch it to extremely but then again we can stretch out our voxels so let’s say something like oh I Don’t want to have the say the the amplitude that’s what I want to work with first now this work with a frequency let’s just say the frequency equals so I’m just imagining you well let me just type let me just type right make it up creative coding okay the frequency Equals the seed divided by one two three there we go if frequency less than can you have it a minus number well let’s just leave it let’s just leave it we should make a function ready to tie this code up so let’s say terrain features okay so avoid or a method terrain features And then here we go and mess around with the frequency so we can mess around with these variables because they’re all public frequencies up here seeds up here so we can use it in any function we like okay let’s let’s see what happens that so I can’t remember what on earth oh Well I don’t know oh I haven’t publicized this or I haven’t saved the script say there we go I think we’ve broken something it’s just gone black oh now it’s working oh my goodness right invalid we’re getting invalid positions look at this so that was a disaster let’s just Think about explicitly what I was doing so what does the frequency equal ten by ten in the inspector what is it it’s 2010 so the frequency I was dividing by these or by these massive numbers that’s after make sure I’m doing this after we’ve got my seed Yes you get the seed so I’m just thinking there you want to make sure that all of the vauxhall farms are getting the same seed before it works out it’s terrain features so that they all get the same terrain features because they’ve got the same seed so I’m Dividing what ten by by this number let’s let’s use a sine function so that’s x it let’s go the other way times by twelve and we’ll say the amplitude by twelve or by three let’s not go crazy although these numbers need to be I’m not thinking about I need to plan this Don’t I beforehand to do like a tutorial on what would actually work I’m just thinking on the fly you would need two meanings meaningfully think about the values that these equations would produce output because you you want your if you want if you don’t want like a flat world or a ridiculously elongated World then you’ve got to have kind of like like maximum minimum ranges on these things and I just want to throw in a load of randomness which is not gonna work so freezing that see there’s not also gonna do I forgot that’s just let’s just run This and see what happens now I’m add what I was gonna do oh this is not looking good again okay I apologize this is a disaster because what’s happening in the algorithm is Devon we’re dividing by the frequency and where we are if we I think we’ve got a massive seed have we I can’t remember where’s I seed seed majeure oh no zero O’s so we’re using zero let’s just put 12 in there I still might be yeah yeah we’re still doing something ridiculous so what I was gonna say is we could use this sine function or the a cosine function because we don’t really Want there to be massive mountains just because the seed number is massive we want to use a property more like is it odd is it even or what kind of what moment in the sine wave which where my hands go which moment in the sine wave Have you got to depending on the journey of the seed number of the number line so let’s use the sine function I think I did something like this in the previous video and we’ll just see the amplitude equals the frequency times 10 the fries 1 see the frequency There we go okay this doesn’t if this doesn’t produce something that looks like something oh no oh no we’re okay okay brilliant Wow we’ve got a big boxy kind of kind of world here it no longer looks like Berlin terrain okay so what’s happening it’ll be very spiky I guess a very low Frequency so let’s try a very low amplitude so let’s I’m now go $9.99 basically the same thing let’s just try it let’s just try beefing up the the amplitude did I save that have a look what else did I want to do so I’m just thinking I’ll pull this quits in terms Of using the okay at least we’ve got difference here okay can I delete these can I dig my way out right at least we’ve got some difference let’s pause this one and just move around in the editor cool that looks kind of amazing but we’ve yeah we’ve got quite a unusual Terrain there there we go so you can see you can yeah I haven’t I can’t claim to have meaningfully used the seed number to change the terrain but at least it’s involved you can see how you might use the the seed number now to to change the features of the Landscape rather than just where you’re deriving the purlins numbers from in a two dimensional pearl in terrain okay so the other thing I wanted to do is make those trees look like trees so I want to do this algorithmically which is different to how I’ve kind of used the Blocks so at the moment the blocks are prefabs they’re just like they’re just objects like this pre-made and they are going to show you the simple voxel both of them they are I’ve used them to populate this array and so the seeding farm script is just referring to that Block that prefab and just instantiating them into the world whereas better delete this soil block just floating around whereas four trees if you look at the my speed coding video this is how we create the trees what is that it’s about 13 lines long and all that’s doing is saying okay for each Block that we place down is there like an 0.1% chance of creating a tree if so we are creating a primitive so game object create primitive primitive type cube and we’re just creating a cube on top of the part of the terrain that we’re dealing with and then I give it a Random height of 24 and then I transform the position of there so I alter the the position of the the tree to being over the correct voxel and then lift it up so that it’s sitting exactly on the surface so what we really want to do after we’ve Done that is generate like a like a big block at the top to look like like a square tree so what we could do is just create another block on the top and just increase its scale that’s what we could do right so let’s let’s do that right so Let’s say now for the head of the tree the crown of the tree they know what’s cool so we’ll say I’m gonna copy all of this hmm in fact I’m gonna copy all of that and we just need to change the names of a few variables and a few dimensions Yeah okay so we’ll call that game object tree head equals that we want a cube We can we just I think we can just Stretch out the kind of like the magnitude of the of its scale so can say scale yeah scale I mean a vector3 scale okay new vector3 for and abdomen dimension for for I think this is gone making sense in my head so I’m kind of stretching it out By nine three or four in each dimensions we’ve got a big block and then we want to yeah have at the same position plus that number so that’s the height of the tree okay that was very happy I’m gonna make sure that I’m referring to the tree Head here oh I’ve missed one there oh no I want that to be tree babe because I’m using I’m using the the the tree babe like that the stalk of the tree trunk of the tree to orientate the position right hack together that felt really dirty and horrible oh my goodness okay Oh now I’ve got this ridiculous seed number let’s so you can see what the earth is going on it looked like that the the tree that the tree head was floating way up high and didn’t look very big at all so my scale function didn’t seem to work one Earth is terrain features so I’m not gonna go and do terrain features anymore let’s now run that again so we can go and inspect a tree gonna find a tree and see what’s going on okay so it’s floating really high it might be big you know okay so I’m pleased that it might Be quite large it’s just cause it’s quite far away although or is it no it looks like it’s lining up so this doesn’t seem to be working tree head oh yeah no tree head tree head transform local scale I mean to my mind you would just scale it by a scalar and presumably This one worked because it was asking me for a vector3 yeah well can I just say local scale x equals four so this should at least make large tree heads yes I kind of look like really bad mushrooms right so got a tree head almost there so Now we just gotta adjust it down So y+ adjust Plus that maybe it’s not / – what was that that was the yeah that was a height of the tree no just make sure that I there didn’t seem to be a change there I had to save the script oh I’m adjusting the adjust value so I Didn’t need to adjust that so actually do you need to have that there but I don’t need to change just okay I’m very sorry I’m not really commenting as I’m making changes there so I’m not really explaining myself very well okay so it’s it’s closed it’s closed now I think what I think I’m doing is I’m pointing to the screen currency I think I’m I’m adjusting the height of the tree head by by the hole made by the whole length of the tree trunk where I need to be doing half that amount okay I think that’s a Gist of it something like that this is very dodgy cuz clearly I don’t I don’t know right / – F but if this if the tree heads do now sit on top of the tree trunks then then something in me knows what it’s doing but not my my brain Okay right that’s kind of work got ridiculously look looking trees so we just asked the point asked the question why do they look stupid look stupid one because of their color absolutely change the material I’m not going to do this video but they’re too high the or rather The head of the tree isn’t matching kind of its high this looks okay so we want to relate the scale to the the height of the tree okay so at the moment we’re saying we’re just saying the head of the tree is going to be able to scale of Four and up here so that the tree point Y component such as its scale in the Y direction of the trunk is is being being assigned here a random value maximum of 24 meters high so where are we we want to say the scale is so if we if it was About what was that about 4 4 2 4 so it wants to be the height of the tree so if it’s four it wants to be 4 so / yeah that’d be really bad oh my god if you know any mathematics must be thinking he’s an idiot And I know I am I am an idiot we don’t a match exactly / well that’s before but as it kind of as it kind of gets bigger then will it get will the scale need to get bigger proportionally that’s just let’s just have a straight correspondence between torque the height Of the tree and the scale I love it see being done has its advantages right you get awesome things like this now they look like good mushrooms no divided by four ah no I’m Santa cook plus four okay so we’ve got a bit of division in There and a bit of addition so they’re gonna have different proportionality kind of consequences that makes any sense yeah so our short trees might look a bit big in terms of their heads and and the real tall trees might look a bit small or something okay but we’re that’s more like it That’s more like it the taller trees have larger heads and the short trees look okay okay yes that’s giving me the sense of a of a tree now oh I’ve done that I’ve gone down the gap below that might be the end of the video okay so we’ve coordinated Some seed numbers we’ve done a bad job in many things we’ve tried to meaningfully involve or show you the I’ll show you the door to how to meaningfully involve the seed number in the in the terrain generation and we fiddle around with the trees there to Look a little bit better and we solve we’ve I’ve also shown you the door to how to solve this layer problem if you wanted to dig down okay in the comments let me know what you want me to do next or I might just stop this project now if Not okay thank you very much for watching I something in there was helpful Video Information
This video, titled ‘Minecraft Tutorial in Unity: more terrain layers, seeding, and better trees’, was uploaded by Red Hen dev on 2018-03-24 15:53:53. It has garnered 650 views and 10 likes. The duration of the video is 00:53:42 or 3222 seconds.
A tutorial for how to manage a seed number across multiple terrain objects, and how to involve the seed number more meaningfully in the features of the terrain.
NB. I’ve kept in my mistakes and debugging — hopefully more informative to watch, and occasionally more enjoyable 🙂
I’ll also talk through a simple and very quick method of adding layers to your terrain, so that you can dig down to a reasonable depth.
Finally, we take a look at how to add ‘heads’ to the tree-trunks that I coded in a previous Speed Coding video.
Thanks for watching 🙂