Part 4 - Tilemaps with Collisions: Make a game like Legend of Zelda with Unity and C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right welcome back so last time we left off we have a situation where we have a four-way movement system where we can move our character left right up and down they're animated and it's set up so that if they go diagonally they're not traveling any faster than if they go left right up or down so what we're gonna start doing today is we're going to add an actual world for our character to interact with and I'm gonna go over how to do this two different ways the first way which is what I'm going to cover today is using unities built-in pile map system I'm not a hundred percent happy with how unities tile map system works but it's not that bad and it's so much better than what they had before 2017 so I'm going to show you that way and then tomorrow's video is going to be about how to do it using a different program and then importing that map into unity that other program is called piled so let's get started the first thing we want to do because we want to take a look at our art folder and we want to find our overworld in that art folder so I have art GFX overworld I want to set this to be sixteen by sixteen but this has more than one sprite on it so in my sprite mode I'm gonna change this from single to multiple and I'm gonna go to the sprite editor it's gonna ask me to apply it now the way this is set up right now is every sprite is on its own 16 by 16 grid so we can just go to slice we're not gonna choose automatic we're gonna choose grid by cell size and our pixel size is going to be 16 on X and 16 on Y and then go ahead and click slice and you can see it sliced everything up already so if i zoom in here plain old grass there's some rule tiles rule tiles an edge some little forest tiles everything gets sliced up pretty well so I'm gonna click apply and we're gonna let that be that for now and it's gonna think for a second here and once this is done we're going to create a 2d object called a tile map so in my scene here I'm gonna right-click I'm gonna choose two diab jecht tile map and it automatically creates a grid to go with it and a base tile map I'm gonna rename this tile map I'm gonna call it ground how about now in order to use this I'm going to need to have a tile palette so to do that I'm gonna go into my window here and I'm gonna choose tile palette from the windows this is gonna come up I'm gonna dock it somewhere that makes sense I'm gonna dock mine right down there and create a new palette in the drop down above so I'll do that create new palette I'm gonna call this ground good sizes rectangle set sizes automatic I'm gonna choose create and then it's gonna ask me where I want to create it to I'm gonna go to my assets folder I'm going to create a new folder and I'm gonna call this tile palettes and inside that tile palettes folder that's where I'm gonna make it just so I can see everything nice and organized no I have my tile palettes folder and drag tile sprite or sprite texture assets here so I'm gonna grab that overworld and drag it right on it's gonna ask me where I want to save this I'm gonna save it in my tiled palettes so I'm gonna choose that and it's gonna take a minute or two depending on exactly how many assets in this case we were we have a thousand seventy three things in our tile map so it's gonna take a hot minute so I'll fast forward through this okay so that was more than a minute but haha we're back so what I'm gonna do here he's just kind of go over the basic functions of the tile palette so I'm gonna choose just the plain old grasp tile way up here in the upper left and automatically it chooses the brush function now the brush function you can use to just paint you can paint it wherever you want to or characters behind it but we'll deal with that in just a second actually let's deal with it now so I'm gonna go to my player choose my player object and then I'm going to change their sprite renderer right now they're sorting layers default which is the same as these tiles I'm laying down so since I added the tiles second unities putting them above the player so what I'm gonna do is I'm gonna change my sorting layer so right now it's set to default I'm going to choose add sorting layer I'm gonna add a new sorting layer by clicking that plus button and I'm gonna call it player now when I go back to my player object I'm going to choose sorting layer again and this time but the playgrounds player the way these sorting layers work is whatever is lowest gets drawn last which means it appears to me on top of anything above it so there we go all right you know back here now this isn't necessarily the easiest way to fill these in it's just by painting everything on it can work just fine for some things but it can get a little tedious so what you can do instead now if I undo that I'm not doing a bunch of stuff I didn't want to do but that's okay gosh I'm dumb sometimes you can use the box fill so let me just redo my player being on the right sorting layer here because I'm an idiot sometimes okay so if you want to you can choose this box fill and what this will do is just create a box that is already filled in this can be pretty much whatever size you want to or if you were using the brush so you drew up over and down you can use the Paint Bucket tool to flood fill that area so these are all things that you can keep in mind as you're making your map now I'm not going to make this super big because I want to focus on one map area at a time so I'm gonna make mine probably don't want to click there why is it doing that that's so weird alright so I'm gonna make my map area maybe double in both directions the size of the screen so I'm gonna go from two about it see you there that's pretty good and I'm gonna make my map in these kind of discreet sections so we can talk about how to make the camera move the way it does that really really characteristic way it moves in Legend of Zelda okay so I got my grease my grass down as a base you know there is a rule tile that you can use that's only available through unities get though and I want to go with just the simplest version possible so I'm just gonna be using this version here and right now I want to focus only on the ground elements not any collidable elements so I'm just looking at paths right now so let's see here I've got some dirt path elements down here so I'm going to make a grab one of these dirt path tiles and start to make an area for a little path and then this is gonna go in the center of it like that and then I'm just gonna kind of work on these ground elements for a second I'll make like that path it kinda goes up and maybe over so I'm gonna fast-forward through this I think okay sorry for that weird cut so now we're going to add our collidable objects here so to do this I'm gonna go to my grid I'm going to create another a 2d object the tile map and I'm gonna call this one collision and I'll leave that right there on my active tile map I'm gonna choose collision so that I'm working on the collision tile map and I want this to appear above the ground layer so I'm gonna change the order in layer two one I could add another sorting tire but I don't necessarily think that's necessary at this point so I'm gonna add some objects on here for our player to collide with so you start with this house you see how I kind of box selected the house it looks like it's only gonna do one of them but if I go down here and click and drag I can create the entire house so there we go let's see let's make some little rock formations kind of here and maybe another one down there alright and let's make some bushes here and oops didn't mean to do that last one here and then since I want to have these be discreet entrances and exits to this section I'm going to use these kind of forest styles here to create those so go all the way down almost there I'm gonna use this to cap it off and then this will turn around mm-hmm where is that down there it is and then I'll have this go across like that and I'll cap it off here and then I'll do the same thing so I'll fast-forward through this so you guys don't have to listen to me talk to myself so yeah I'll meet you back here okay so here I've got my base everything on the ground and then everything I want collidable I have on one layer now to make this collidable for our player I'm gonna go down to with collision selected add component and I'm gonna add if you just type four so tile I'm gonna go with tile map Collider 2d and then what you'll notice right now is that every tile that I added in that tile or in that collision layer now has this green line around it to represent its collision now this isn't super well optimized so I'm gonna do something else to make it more optimized the reason it's not optimized cuz I have all of these pieces it's all of these individual colliders whereas if I had it as one big one it would be much better so it's optimize it I'm gonna go down here and add another component I'm gonna add a rigidbody oops 2d I'm gonna set this to static so that I don't have to worry about gravity or anything and turn off simulated um oh no I think I still wanted to be simulated and then I'm gonna add another component which is called a composite Collider which is gonna take all of these separate colliders and mash them into one at least it's supposed to oh yeah yeah one more thing I have to do in my attala map Collider I need to check used by composite and that should mash them into one maybe hmm that work the way I wanted it to let's find out so if I hit play let's see if our collision is working like it should ok I was pretty awesome all right so let's fix that so let's go to our player really quickly and in our player we want to find our rigidbody 2d we want to open up the constraints and we want to freeze at the z-rotation let's try that again um all right so if I walk down so my collision is working ok with that and because I have my Collider so small I don't have to I didn't have to worry about making the boxes tinier except I notice a few things I need to fix right now so let's go back to our overworld and let's go to the filter mode change it from bilinear to no filter point click apply if you'll notice the world looked a little fuzzy around our character whereas our character looked really nice and sharp there we go all right you know if I hit play let's see how that looks now yeah that's better so our character is able to walk around our world pretty easily no we can't see the whole thing so for now we're gonna do a little bit of a hack we're gonna take our main camera and we're going to parent it to the player so I'm just gonna make it a child of the player I'm gonna set its position to zero zero which centers it directly on the player now if I hit play cameras gonna follow the player around it's not a true Zelda camera because I can go down here and see outside the world so we're gonna fix that relatively soon I just want to walk around and check that collision is working the way that it should now one thing that you may or may not notice if I do this so I'm gonna go down here to the bottom and I'm pressing down I don't know I guess it's not doing it on that one if I'm already colliding with something and I want to move in another direction it acts a little funky there's like a drag to it so what I want to do is make that a little more smooth to do that I'm gonna go to my assets here you can all right click I'm gonna make a new folder so create folder I'm gonna call this materials and inside the materials folder right click I'm gonna create a new physics material 2d I'm gonna call this slippery and I'm gonna make the friction significantly less I don't want to go to zero so I'll go to like say point 1 now when I choose my player I'm gonna find the box Collider 2d and I'm going to apply this material to that which is gonna make our player have less friction with the world so let's try this out now so yeah that's already significantly smoother you could make it be zero friction if you want to so here's one way that we can set up our world here now we're gonna be changing this well not changing this I'm going to show you another way to do this using that second program that I told you about tiled and then after that we're gonna start dealing with the camera so that the camera doesn't go outside of its bounds like it is right there and so that when we reach the edge we have this nice neat zelda-like transition to the next map area so thank you very much for watching if you have any questions feel free to leave them in the comments down below you can follow me on Twitter to find out when I post a new video you can you have my discord where I'm chatting pretty much every day and yeah have yourselves a wonderful day
Info
Channel: Mister Taft Creates
Views: 143,985
Rating: undefined out of 5
Keywords: Unity, Unity Tutorial, C#, C# Tutorial, Zelda, Zelda Tutorial, Zelda Unity
Id: zA8wa-L5xek
Channel Id: undefined
Length: 16min 8sec (968 seconds)
Published: Fri Aug 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.