Simple Random Forest Generator (Unity3D, C#)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you hello and welcome to another quick tutorial and this one we're going to create a sort of randomized semi procedural forest thing using some free assets that I found I think I still of the window open I don't one second using this low-poly forest puck that I found by David Sten falls so if you like that I'll leave a link in the in the description below go check that out so all I've got so far is I've just got a ground which is just a quad with a green material on it and I've stuck a little just a little controller on the camera just so I can move around so I mean you won't be able to tell anything because it's just a plain green ground but I can look around left and right not up and down and I can move around that's the only thing I've done to set things up well I messed around with some post-processing stuff to make the visuals look a bit different but that's completely irrelevant to this tutorial so just ignore that so if we're going to our scripts folder I'm gonna create a new script I will call this forest generator for lack of a more imaginative title and we'll go straight into here so the first thing we would do is we want a public int and we're going to call this forest size and just by default we'll say 25 when of course you'll be able to change that in the actual inspector and then we're gonna need a second public int which were going to call element spacing now an element I'm gonna set this to 3 this is based on values that I come to while messing around with this script before doing this video so I'm not just pulling these numbers out of thin air and they happen to look about right I have actually worked this out before an element is going to be a thing that we place in the forest so for the most part is going to be trees but it could also be rocks small plants whatever you decide to put in your forest and then we're going to need a public class so we will call this element and it needs to be serializable because we're going to be entirely setting this in the inspector so just for our own ease we'll give it a name string so we can see what type of class a type of element this is and then we're going to give it a game object which we'll call prefab so then we'll go back up here and we're going to create a array of these elements now if head back into unity will create an empty object which we'll call forest we set the position on that and drag our forest controller generator on here so we have our elements array right now there's nothing in it so we're going to create one element which we'll call trees and I have a folder here full of prefabs that I've set up for this because the the prefabs in this asset they're a little off in that the origin point of each model is like halfway up the tree so it's it's a bit awkward because the it's not always dead halfway so I can't come up with code to place it at the right height so I've gone through and created an empty game object and then placed the tree inside it so that it's at the right height so we're spawn we're gonna be spawning in this game object prefab that has the tree model inside it at the correct height so we'll just pick one of these at random and then we'll put that in there and then we'll go back to our script and we're just gonna create a loop I'll just copy that and change that for Zed so we want to be getting our element which at the minute we only have one so we'll just say element element equals elements zero because we only have one element and then we want to position which will be a new vector3 x zero because everything on this test world is all flat there's no terrain to worry about if you were applying this to say some standard unity terrain you'll be able to passing the terrain height float to this here so you'll be able to get the height quite easily whatever or whatever terrain system you're using or measure basically whatever height you wanted to put it out you just feed that into there and it would take care of it from there we're going to create a new game object calling new element equals instantiate element dot proof up and then we're going to set a new elements position to position so now if we go in here so sorry I'll just clear this up so we're looping from zero to the forest size that we've set obviously and we're incrementing by element spacing so that means that currently we've got it set to three so that means that every three units in world space we're going to put an element down and the minute we only got one element so the element is always going to be that same tree but that's what's happening basically we're looping in a two-dimensional grid and every three units we're going to put down a tree so there we have a very very dull uniform looking forest so let's start to make things a little bit more interesting I'll take maximise off so we can look at it in the same view as well to start with let's add another vector three and we're going to call this one offset and it's going to equal a new vector3 and we're gonna do random range well just put a hard-coded in the minute we'll just put a hard-coded in number for now but you could you could tie this in to your element spacing you could have it be element spacing divided by two or something like that but for now I'm just gonna for the sake of speed I'm just gonna put in a hard-coded number so put minus 0.75 F to 0.75 F then we'll just copy that again we don't need anything on the y-axis because everything is flat and then that's that so it's just gonna give us a vector offset that's a random amount it's just enough to vary things so they look a bit less uniform but not so much so it's gonna encroach on the next tree or element or whatever because the three apart so that could get close to each other but they're not going to touch each other so then all we just do there is we add our offset to our position if we try it now our tree should look a little bit more organic so there we go I'm just try and find a tree there we go so now they don't look so like gritty a little bit more organic looking but we've still got a ways to go so we're gonna add another vector three and this one we're gonna call a rotation as you may have guessed already we're going to create a random rotation so on the x axis and the z axis that's essentially tilt so we don't want a lot of that because you could end up with trees that are like a 45 degree angle which maybe you're going for if you're going for like a creepy forest or something like that but we don't we don't want that in that way in this we're just making a fairly average looking forest however the Y rotation that is basically a vertical rotation so that is if you were to take the tree and turn it then this is the rotation that you would use and for that we want to have the full range of motion wanted to be facing in any which direction it wants so for that one we're going to set it to F and now that we have that we'll go down here and we're set Euler angles try that again we should once again have something a little bit more so now our trees don't look all so straight and stiff and obviously we have our room offset placement so it's starting to look a little less procedurally placed which is what we're going for and the last thing we're gonna do here before we work on something a little bit different is we're gonna give it a slightly run to my scale and the way we're gonna do this is we're gonna set it to vector 3.1 so that's basically the same scale as it is 1 1 1 but then we're going to times it by a random dot range and we will go 0.75 so that will be 3/4 of the size that it is to 1.25 so that would be 1 and 1/4 the size there it is and then nice and easily we just said that transform local scale equals scale and just to keep everything neat we're also going to set the parent to the poorest generator object because we don't want tree salt floating around the hierarchy so there we have slightly different rotations slightly different placement slightly different scale on each tree just makes things look a bit more interesting but it's still a pretty boring forest so let's let's try and make things even more interesting so so we're all back down to our element class we're going to change this to an array I'll call it prefabs then we're gonna create a simple class to get easily get an object out of that array so this is a public game object is just going to return a game object and we'll just call it get random and all we the point here is return prefabs and then for the index random range 0 to prefabs length and that's all that does and then for this we just want element get random and then if we go to our forest and we see this now we have a an array instead of a single object so if I just put the one tree back in there I can't remember which tree it was but if I just put the one tree back in there it doesn't really make any difference to our forest as you can see we've still got just a bland everyday forest with slightly organic looking trees but still pretty blunt but if we change there's so many trees if I got here one to six so if we change this to six and then I drag each of these tree prefabs and then I run it now we have something considerably more messy this will pave Oh would be a good word to describe it but it looks a lot less like it's just some computer-generated fake forest thing so or add another layer of organic miss to it organic looking this to it that's not a word to it by we're gonna create a public bull well absolute first we need a public int which we're gonna call density and we're gonna lock this to a range and the range is gonna be zero which absolutely no it's not a zero because zero then you don't need it there at all because that one place any to ten and the real and we need to it like so you could change this to say for example a hundred if you wanted it doesn't have to be ten if you want it to have like a more finer tuned control but basically all we're gonna do next is so for a public bull of what we'll call it camp place and all we're gonna do is if random range zero to ten is less than and then whatever our density is then we return true else we return false and what this is doing is is every time we get a random number like I say you could change this to a hundred you'd have to change this to a hundred as well if you did that but you could have it higher and you get off a finer control over it but every time we check this come place we're gonna get a random number between zero and ten and if that number is below our density then we place it well we return true in this case so it had the higher the density they more likely that it's going to return true which means when we put our if check in here the more likely we're going to be placing something so up here because we don't want to do any of this if we're not going to be placing it so up here we're just gonna put if element dot can place and then we'll just close all these in brackets and we'll go back in here and we set our density we'll give it a we'll just give it a value of five for now now as you can see there's far fewer trees because our density is about 50/50 so about half the time it's not going to be placing trees and we can help that to eight for example and we'll have five Maltese but still not as many as if we had it all the way up to ten so I found six to be quite a satisfying number for the trees I don't know obviously I can increase the size of the forest to make it look a bit more interesting but we're still not done so as you can see this was an array this is an array here which implies that we're going to be putting more elements in than just trees which we are so first let's go back to our code and write what we need to do to handle that so we're going to follow the for loop here and we're going to put for int I equals zero I is less than elements dot length and then just I plus plus I want to enclose all of this inside that for loop and then instead of equal in the first one it's going to equal whatever our current iteration is in the elements so the one thing that we do need to do now is because we're basically we're giving priority so we have our array here trees of currently first and then we've got some plants and some rocks so we're going to put plants and rocks as a next two elements we're giving priority to trees so basically that means if there's a tree there we can't put anything else down there's already something in that spot so we need to go down here and put a break so that if we have placed a tree we're not looping through to the next element we're just skipping out of this for loop and then going to the next coordinate and check in what's there so with that in mind let's go in here increase the size of our array call this one plants and how many plants do I have to and then for density for plants we want that to be quite high so I'll put that to eight and then our final one rocks so and remember the priority works all the way down so if there is a tree we can't press a plant but if there's a tree we can't place a plant there if there's a plant we can't place a rock there if there's a tree or a plant we can't place a rock so rocks always come last if rocks are most important to you when you are doing this then you want rocks to be top of the list and then density for rocks I'm gonna put quite low and now it's a far more interesting for us to look at although it still looks a bit sparse when you can see the horizon so what size is my ground ground is 500 by 500 so let's make this let's just make you 500 and now the frame rates gone down obviously if you were if you're doing this properly you'd have level of detail and you you wouldn't have 500 by 500 worth of forests all at once but you get the idea I'll just lower that down a bit and we can have a look at it full screen but that is essentially everything so I mean you can mess around with the details you can add more elements like I say if you wanted to put this on actual terrain you would just pass in a height value where I've got zero F for the Y you would put in an actual height value so that any given element is placed at the correct height but other than that this this is a fully functional random forest generator e thingy so hope this was in some way useful for you let me know if there's any other thing like this they'd like me to to go over obviously I will get back to the Minecraft stuff this isn't taking the place of that or anything I just like to do these little videos to break it up every now and again next week will probably be a Minecraft video again but for now that is everything I hope you enjoyed it hope you found it useful and I will see you next time bye bye
Info
Channel: b3agz
Views: 9,331
Rating: undefined out of 5
Keywords: Unity3D, Procedural, C#, Coding, Tutorial, MadeWithUnity, Low Poly, Toon, Random Generation, How To
Id: 604lmtHhcQs
Channel Id: undefined
Length: 19min 52sec (1192 seconds)
Published: Mon May 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.