Unity Farm Game Tutorial - Planting & Harvesting - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there it's silverley b here and today i bring you this tutorial on how to make this very simple little farm and this is what we're going to be doing today this tutorial in particular is about doing the tile and big plotland where we're gonna have the farm on our next tutorial we're gonna be seeing how to make this farm with isometric tiles and on the third and final tutorial we'll see how to use scriptable objects and how to load this resources into unity so let's begin for starters we have here a very simple 2d project that we've created in unity the first thing we're going to do is create a sprites folder so let's go to create folder and name it sprites very simple the first thing we need to do is to import these sprites in this project the sprites are available down in this description link below it's on github and you also have the access to the code used here so let's begin go to import new assets and choose the items now that we've imported the items we need to adjust the sprites to the project the pixels per unit is going to be 198 and the sprite mode is going to be multiple and i like to adjust this 2.0 filter even though the sprite is very big so it's 1920 by 1920. i still prefer to go with point no filter i think it's maybe foolish of me because it's so big it doesn't matter but i do prefer i'm gonna hit apply and i'll go to the sprite editor in order to edit the sprites so what we have to do is we're gonna slice automatic the pivot is going to be on the bottom we can then hit slice and we gotta adjust a few things first we can change the pivot of the blot of land to center it's important though that the blend sprites remain pivoted to the bottom because they'll all be growing from there and that's going to guarantee that they don't lose their position when the planned stage changes and then we can make the same changes to the icons as well now another thing with candor this is not necessary but i prefer is to name this price accordingly so this is going to be corn zero this is going to be corn one this is just to make it easier for us it's not necessary if you'd like to skip this step you can go ahead and skip it now that we've finished it we can hit apply and all the sprites will be here the first thing we gotta do is to start with the farmland where we're gonna blend all these crops and let's begin by dragging and dropping this form item we're going to reset its transform and we're going to name it plot okay so here we have a plot of wrench we are also going to change the color of the camera because i think it's better if it's another color maybe a darker one i like this shade you can choose whatever suits you and so now that we've chosen the color let's work on this flat around what's going to happen we're going to have a little plant that is going to be a surprise surprise planted on this flatland and it's going to be like tall or navy slower it's gonna crawl in it until you can harvest it and in order to begin this cycle we're gonna start by making the scripts folder we're gonna store our scripts scripts and let's begin making the plot manager create chart script and we're gonna go with block manager this will be managing the heat plot of land that's created let's open it on isro studio so here we have a plot manager we're going to start just by having a on mouse down event where we check for the players click on this plot of land so we're gonna have debug.log clicked very simple if we run this right now you're gonna realign it's not working very well well first because we haven't got the script on this plot now here we can see the block manager but it doesn't work why because as of now this plot of land does not have a collider and ecology is going to be what's checking for the click so let's add a collider i'm going to go with a polygon collider but here you can see that it has 73 points and it looks a bit janky because it's getting all these points for instance here it could be just a straight line and it's going in for all those vertices and it's not necessary so what we're gonna do is we're gonna rewrite these points we're gonna say it's six points that we need in order to capture this whole collider we're gonna start with zero which is here we want to get it here and we're gonna adjust it i think it's 1.5 let's see now it's 1.4 no 1.3 1.25 i think it's good 1.25 is looking good and here we're going to get this point it's going to be on the x 1.82 okay one point h 1.666 yeah it's going to be 1.66 with 0.4 it's looking good this line and now that we've discovered this and this point we can start by just giving all these other points on the x or the y axis so for element two we're gonna have the same x but mirrored y so it's gonna be minus point four and here we're gonna mirror this one it's going to be 0 and minus 1.25 on the element 4 we're gonna mirror this point on the x-axis so it's going to be minus 1.66 with minus 0.4 you get the js so here it's going to be minus 1.66 with 0.4 we have a collider here we can test this out and check to see if it's working here you can see the message so it's finally working because we have the collider now one of the ways this is going to work is it's going to plant something if it's not planted or harvest the crop if it's planted and if it's mature so what we need to do here is we're going to head over to the plot manager script and on here we're going to have a variable pull as blended false it's going to start false because the plot is going to start bare and maybe you plant it later and what we need now is also an object that is going to represent the plant so we have a public game object plant and here this plant we're going to set it active when we plant it we're gonna set it inactive when we harvest it so let's go over here we're gonna say f is planted then what we need to do is we need to harvest we're going to separate this harvest in a new function we can call it harvest here and let's create it here here we go we're also gonna need a void planche here we go so what we're gonna do is just say debug.log harvest and we're also gonna set the object of the plant inactive so blend dot set active equals false and another thing is it's planted is also gonna be false because we just harvested it so it's not planted anymore so here we have harvested and here we have plant foreign here we go on here we're gonna just do the same except we're gonna replace harvested with plantage and we're gonna replace false with true so let's save it and test this out on energy we hit play and when you oh sorry i haven't set the game object here we're gonna set this order to one and we're gonna set this right to this corn to be a very pretty corn we're gonna change the name to match okay so here we have the corn and we just need to reference this game object here let's hit play and check this out oh sorry we should start like this okay wait a second here okay great that's good you hit here plant harvest plant harvest and if we look over here there is click there is planted there is harvested now what we need to do is we are just planting a full grown corn crop what we need to do is we need this corn to go through these stages in order for the core to go through these stages we need a reference to these sprites and we need to change this plants right so in order to do that we're going to go back to the code and we're going to ask for the public right and we're going to ask for the plan stages these are going to be the sprites for the plant stages and we're going also going to keep an inch that is going to be planned stage plant stage is going to start with zero gonna be in which stage the plant is in we're also going to keep instead of a game object we're going to keep a sprite renderer here and over here we're going to keep a timer in order to keep time off the plant stage first we're gonna need a float time between stages we're gonna set it to two f so two seconds per stage and here we're gonna have to keep a timer okay so here instead of plan set active we're gonna say plant dot game object that's an actor because plant is now a sprite render and it's not an object anymore so what we're going to have is one implant we need to set is planted to true we need to set the stage plan stage to zero and we need to update the plant appearance in order to do so i'm gonna create a new function void update plant i'm doing this here because i know that later down the line it's going to get more complex than it is now so i'm making this a separate function so update plant is going to be plant dot sprite equals to blend stage just plant stage whenever we plant we want to update the plant okay so another thing we need to do is we need to set the timer to the time between stages and start counting it so what we need to do is timer post time between stages and here we're gonna keep lowering the time minus equals time dot delta time and if timer is lower than zero but first let's check if it's planted because if it's not then we aren't going to need all this so it is planted so if there is a plant in our garden then we're gonna count the time and we're gonna say if timer is less than zero and it's not on the last stage so how do we know what the last stage is we just need to say that plant stage is less or equal to plant stages dot length minus one this gives us that plant stage is lower than the last plant stage so what we've got to do here is we're going to set timer equals to time between stages and we're going to say stage and stage gonna get bigger and we're gonna update the flat that should work let's head back to unity and adjust it so here we have this plot we're asking for the glance stages which is going to be this let's lock this on the inspector and select and drag and drop here now you can see that the plant is mismatching this is because we changed from game objects to sprite render we just need to drag and drop it here again and it should work so let's check this and now we have a corn that's growing beautifully and we have an index out of range error we just need to fix one little thing it's not less or equal it's less let's save this and head back over to unity let's play again start and this should work with no errors and it does and it does this is great we have a corn but we also need to see that whenever we click it harvests and we don't want that we just wanted to harvest on the last step and we know how to get to the last step let's do it again we just need this information and we need to say here that harvest can only occur when the plant is ripe so here what we need to do is f plant stage then here we go okay so if it's planted it's going to try to harvest it we're going to need to equal here and if it's on the last stage it can harvest otherwise it won't harvest let's say let's delete this we don't need it anymore it's just littering our error log so let's save let's check this out back on unity here we go you try to click it's not working it doesn't do anything but it's the last stage let's check this out it works now what we need to check is we can plant this little corn and it's gonna grow and when we click over here on top of it it is not going to respond this happens because well the plot has a collider here as we can see the plot has a glider but the plant has no collider and it plunged even if the plant had a collider it wouldn't know what to do with it it wouldn't have used it to sense the click and to make the plot sense the click on the plant collider what we need to do is first let's go here to the plot and add a component e rigidbody 2d it's not going to be dynamic or kinematic because it's not going to move so since it's not moving it's static and we can leave it just like that if we go over here and edit collider we can see that when we click we can try and click here you might not be seeing it because it makes no difference but even when it's small the plan still has this very large collider so here when we click it's easier to see if we adjust this to a smaller corn plant i'm gonna reset the collider and you can see here that it's very small and even when the plants gonna get bigger the collider is gonna stay small so when i click here it doesn't work why is this happening because unity's collider doesn't adjust in real time but in order to fix this because we need the collider to be adjusted in our with the plant size we're going to use the update plant method and we're going to keep here a box collider sorry box collider 2d that we're going to call plant collider this is going to be a reference to the plant collider and over here instead of having a publix price render for the plant we can say that the plant equals two transform dot get child zero and we're gonna get component and it's gonna be spread renderer this way we don't even need to set this up the game will do it for us and the plant collider is gonna be the same thing but instead of a sprite render we're gonna go with a box collider okay we're doing great so far what we need now is an equation that is gonna adjust the collider size to the sprite size and in order to do that we're gonna go plant collider dot size equals to let sprite dot bounce dot size this should do but the thing is the plant collider will be off-centered because we have a sprite that is centered on the bottom so in order to adjust this collider size all we need to do is change the offset of the collider so we're going to go with plant collider dot offset it's going to be equal to a new vector 2 this means two dimensions and since the offset is gonna be felt on the y-axis we're gonna go with zero for the x-axis and we're gonna go with lent plant size dot y this means vertical size divided by two okay this should work let's check this out to see if it's working correctly i'll hit play yeah we're not working correctly oh no sorry sorry sorry sorry i actually forgot that it's not planned that size it's planned dot bounce divided by two if we go back to the gain you can hit start and check to see and it seems like it's working correctly so there we go we can plant it and we can harvest it even if we click up here this is great that was it for today and that was it for this tutorial and if you'd like to keep on watching these tutorials the next one is going to be about making the style board where we're gonna set the farm and it's gonna be made out of these tiny plotlines that we've just created today and on the third video we're gonna see how to make all kinds of plants so please stick around and click on the video if it's already out you can watch it right now that was it guys thanks for watching don't forget to leave a like subscribe and comment this helps a lot thank you bye
Info
Channel: SilverlyBee
Views: 9,445
Rating: undefined out of 5
Keywords: unity tutorial, unity, game, game dev, games, tutorial, game tutorial, farm game, farm sim, farm game tutorial, game development, planting, harvesting
Id: gEnogKcRf-g
Channel Id: undefined
Length: 24min 2sec (1442 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.