Create a grid in Unity - Perfect for tactics or turn-based games!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll show you how to create this nice checkerboard padding grid as well as highlighting on mouse over and how to select individual tiles so you can perform your game logic let's get stuck into it begin by creating two c-sharp scripts one for the grid and one for the tile itself in the grid manager scripts let's create a few variables one for the width of the grid and one for the height let's create a function called generates grid in here we will loop over our width so let's call this x for width and then inside here we will loop over our height so call that y and now we need a reference to our grid tile so let's create a reference there and we'll call this tile prefab and before we spawn that we need to actually create our tile so back in unity let's create a 2d object sprite square call it tile put our tile script on it and then let's make that a prefab next let's create a empty object here call it grid manager slide in our grid manager scripts and for the width i'm using 1080p so i'm just going to use a 1080p aspect ratio so i'll do by nine and whoops then we'll put the tile prefab on the tile slot there and now let's actually go back and spawn them so now we're looping over our width and height say spawned tile equals instantiate and we'll send in our tile prefab u vector 3 and this will be on our x and our y and then just quaternion identity is fine let's name these so they are more self-explanatory in the editor so spawntile.name let's call this tile and then we'll send in the x and the y just in our start method oops it's called generate grid all right let's try that out and there we go so uh zero zero tile is actually on zero zero and instead of moving this to center in the camera how about we move the camera to center into the game world so that r0 is actually in world space zero zero so let's grab a reference here to our camera and actually the transformer of the camera and down here we will say cam transform position equals new vector3 and we will say our width of the map divided by two and this is actually a float so let's just cast this to a float sorry it's an int we need to cast it to a float and then also we need to remove half of a world tile and you'll see why in a moment okay and then we can just copy this for the height as well heights and then we actually still want the camera to be at -10 which is the standard position that they start on let's try that no we have not set our camera all right let's try that out beautiful but it's a little bit hard to kind of see what's going on so what would be good is if we could grab this tile here and make this one type of green and then this one another type of green and then back to the original green just like a chess board so i'll show you how to do that oh in our grid manager so the way we do that is we say is offset we're going to check if this style is offset and we'll say is x modulo 2 equals 0 which basically we're just saying here is x is x uh even right now and y modulo two uh does not equal zero so we're saying is x even and y is not even or alternatively we're going to say is x not even but why is even and if if if so we want to color them a separate color in our tile scripts let's remove this boilerplate and let's create a few references here so private color base color and also offset color and a reference to uh render right here sprite renderer because we actually need to change the color of it and let's make a public void and knit function and this will take in a boolean is offset and then we'll say render our color equals if we are offset let's set it to the offset color otherwise let's set it to the base color and back in our grid manager we'll say spawntile.net is offsets like that now over on our tile let's create some colors here so i've got a nice green for this base color and then for our offset color i'm going to use the same green but i'm just going to change it just over slightly let's put on our sprite renderer here and let's try that there we go now we can actually see the individual hexes or the squares sorry so let's add a little bit of interactivity here so on tile let's create uh on mouse enter and let's actually grab another reference and we'll create a game object here and this will be our highlights now there's many ways to do this you can do it with shaders or just change the actual sprite itself but i'm just going to enable or disable this highlight object whenever we mouse over it so on mouse enter it's going to be true and then on mouse exit here we're going to say pulse and then back on our tile i'm going to just create another 2d sprite on top of it here and this will be called the highlight and let's just make it a white but let's reduce the opacity here and then actually disable it and then on the tile just add the highlight and then also to mouse on it with those mouse events it actually needs a collider so let's add a box collider 2d there and press play and there we go now we've got interactivity so this is a quick easy way to start a grid uh obviously you can start placing units down here and then you can tap into the mouse down event here to select the tiles or the units and then you can be on your merry way and create your game so i hope this helped if it did leave a like and subscribe and i'll see you next time for the next tutorial uh hello so this is post-production matt and i figured i should probably show you how to actually grab these tiles in logic so the way i would do this is i would create a private dictionary and the key will be a vector 2 and then the actual value will be a tile so i'll just do that now when we're creating a grid i'll instantiate this new uh dictionary and after we create a tile i will say tiles and then new vector 2 and this will be the x and the y equals to the spawn tile like that and now you can create a function here it could be public and this will return a tile get tile at position this will take in a vector 2 and then you can do this so we'll do try whoops no we won't actually we will do if tiles try get value and we'll put in the pause and then we can actually say out ba tile so if if the tile is available we will simply return tile otherwise let's just return null so then you can just quickly just call that to get the tile at the at the current position and that's how you would do that so
Info
Channel: Tarodev
Views: 228,978
Rating: undefined out of 5
Keywords: tarodev, unity, tutorial, grid, unity grid, dynamic grid, runtime grid, unity tutorial, tactics, turn based, turn-based, tactics game, unity tactics, grid tiles, hover tiles, game development, unity game tutorial, grid map unity, grid system
Id: kkAjpQAM-jE
Channel Id: undefined
Length: 8min 56sec (536 seconds)
Published: Mon May 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.