Create a grid in Unity - Perfect for tactics or turn-based games!
Create a grid in Unity of any width and height. I'll show you how to create a checkerboard pattern to better distinguish each tile as well as how to hover and select each individual tile.
Perfect for any tactics or turn-based game.
GridManager.cs: pastebin.com/cip74C4E
Tile.cs: pastebin.com/UD6zE467
=========
❤️ Become a Tarobro on Patreon: / tarodev
=========
🔔 SUBSCRIBE: bit.ly/3eqG1Z6
🗨️ DISCORD: / discord
✅ MORE TUTORIALS: / tarodev
0:00 What we'll make
0:10 Creating the GridManager script
1:00 Creating the Tile prefab
1:30 Spawning the tiles
2:25 Centering the camera on the game map
3:30 Creating a checkboard pattern
5:35 Add hover and click events
7:07 Storing and getting the tiles via script
Пікірлер: 366
NOTICE: If you're having trouble seeing your tiles, ensure your sprite renderers alpha are set to 255. Sometimes it defaults to 0. Part 2 has been released: kzread.info/dash/bejne/mGmkz5RyqcvHhNY.html It includes additional functionality like unique tile logic, game flow, unit moving and attacking! Did you enjoy the video? Let me know so I can continue making helpful content!
@smileychess
2 жыл бұрын
Yup, the alpha channel was the problem. Took 20 minutes to figure it out (even after reading your message here, because I'm stupid). Thanks!
@WorkingDev
2 жыл бұрын
@@smileychess new to unity and this is my first time dabbling in it. Where do you find this setting?
@smileychess
2 жыл бұрын
@@WorkingDev - It's in each of the places that you set the color. It's the last slider, labeled "A" and has a range of 0 to 255. Crank that up to 255 so it's not transparent.
@themonkeysadvocate3265
2 жыл бұрын
@@smileychess *2 hours for me XD
@lillylyddon9869
Жыл бұрын
You should really pin this to the top.
Even the simplest tasks seem impossible if you don't know how unity works, it's like looking at the flying board for a plane
All that in under 9 minutes! Tutorials are on par with the best on YT. Thanks.
Imagine not liking this video... perfect video! Short, clear, easy to follow. Keep going!
The Cameraposition based on the size of your grid was what I needed!
I looked for any sort of video tutorial, blog post, etc explaining this in 2020. Thank you so much!
For the offset bool you could use: (x + y) % 2 == 1
@theroboman727
2 жыл бұрын
also maybe: x % 2 != y % 2
@Tarodev
2 жыл бұрын
You're right! That's much cleaner.
@DuniC0
2 жыл бұрын
Yeah! I thought the same! (x+y) it's called Manhattan distance and with the modulus of 2 you can get a perfect checkboard
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
@overcomeaging
2 жыл бұрын
or: (x % 2 == 0) ^ (y % 2 == 0)
Most understandable grid tutorial ever! Thank you! I had problems with activating hilight object, and solved it with adding hilight color field into "Tile Script", and empty "backupColor" variable, that saves renderer color on mouse collision enter.
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
This is a magnificent tutorial, thanks champ!
Instead of casting _width and _height to floats, you can divide them by 2f, it will result in a float.
Few minutes into the video and I already knew that this is an instant like and a sub
@Tarodev
11 ай бұрын
Hope you enjoyed the rest ❤
Great video, really taught me a lot of stuff in a short time.
Thank you for your tutorial, that's a nice way to create a grid.
wow it in detail works! Very good job
Absolutely brilliant tutorial, thank you very much for your work!
@Tarodev
Жыл бұрын
I'm glad you enjoyed it :)
Great video! straight to the point.
Much love for this!
Thanks for this. This was super good and simple
Thanks for the tutorial, was very cleary and for non english speacker it was very simply to understand you without subtitles.
Very useful tutorial! thank you very much
These tutorials are really clear, concise and you also explain some of the principle behind the code instead of just saying "type the code that I am typing and it will do what you want". I am subscribing. This tutorial is giving me an idea to make a small game for my portfolio, thanks a lot.
@Tarodev
2 жыл бұрын
I'd love to see what you end up making 👌
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
@awildmoose6798
2 жыл бұрын
show me where he explains the principle behind the code. he didnt do it once.
fast, clever video, amazing!
You saved me tons of hours of work!
This is very useful, thank you!
@Tarodev Thank you brother, I learn so much from you.
You deserve more subs thank you for the tutorial.
Thank you for this wonderful tutorial! I was wondering why my colors didn't alternate after I added the sprite renderer and isOffset boolean, turns out my colors were all transparent! Make sure to check whether your Alpha value is at 255 to avoid the same problem I had :')
@pandasempie
Жыл бұрын
LOL I was about to have the same issue ty T_T
@pseudoleviathan5923
Жыл бұрын
Same issue thank you!
@chrisdaly6023
Жыл бұрын
you kept me from facing a huge headache.. thanks!
@goodymicplaysgaming
10 ай бұрын
I was soooooo confused as to why it wasn't working, thank you.
Very good tutorial!
Completely agree. Aweso video
The visual studio text is too small, pretty difficult to read. Other than that pretty good tutorial.
@nakano15
Жыл бұрын
I think they fixed that on 2022, but at the cost of performance hahah.
FACTS!!!!!! Thank you so much man, you're the best!!!!!!!!!
Thank you alot. Im learn how to make a game rn, this guide help me alot
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
Thank you so much!
Amazing!TYSM!!!
rly good video man
you're my fav tilemap guy right now!!
@Tarodev
2 жыл бұрын
Wait until you see part 2 😉
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
Saw your video in reddit, decided to sub, will be back today and get this tutorial going, thanks for this!
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
Thankyou for this
Would look forward to more board game type of videos!
@Tarodev
2 жыл бұрын
I just released part 2: kzread.info/dash/bejne/mGmkz5RyqcvHhNY.html :)
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
Clear, concise instruction without fast forwarding to a speed no realistic human could follow. Probably(why lie? you definitely are) one of the top three cleanest and most efficient coders for Unity tutorials on KZread.
@Tarodev
Жыл бұрын
Damn bro
哇哦,太酷了bro,非常感谢你的教程👍
Personally, I think storing the grid in a data structure such as a two-dimensinal array is nice to work with. To extend this, if we had a three-dimensional array we could store layers of grids, that could represent house floors, underground floors, etc.
@dpurrington
6 ай бұрын
That's the model Minecraft uses. Every square "foot" is of one material or another. Air is just another material that you can pass through.
Nice tutorial
Thanks, good video :)
Wondering if you could zoom in more on these tutorials because watching them on mobile is quite a pain!
Would you be able to give an example of how to use the GetTileAtPosition function? Just to do something like displaying that tiles name to a debug menu or change a clicked tile to a different color?
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
the new brackeys is looking good.
@Tarodev
2 жыл бұрын
You're too kind ❤️
Thank you sooo mch
Thanks alot.
amazing
You should mix in some devlogs that will be sooo cool.
@Tarodev
3 жыл бұрын
Very good idea. I'll do that! Also, I took your advice and started a discord. I'll drop the link in a video or two when it's ready
@noobcraft5712
3 жыл бұрын
@@Tarodev oh :)
@andubunea9627
3 жыл бұрын
@@Tarodev OH can t wait to see the discord I think I will really get help with unity from there
best easy board ever
Like it very much
How would you go about creating an outline around each grid cell instead of coloring each grid cell like you did in this example? I tried adding in an Outline component but this did not work.
Sorry if this is a stupid question, but if I wanted to increase the size of the individual squares and stop them from overlapping, how can I edit the grid manager so it places them, say, 1.5 units apart instead of 1 unit apart? great video!
@emmetttolis9135
8 ай бұрын
Nevermind! for anyone else wanting to achieve this, Change the orthographic size on your camera for the desired effect! Thanks a ton man!
Awesome tutorial man, you just got a new subscriber :) I have a question for you, how different would be the process of achieving this, but in 3d? i.e Final fantasy tactics or Vandal hearts, in which the world is isometric 3d and you can rotate the camera in 4 angles?
@Tarodev
2 жыл бұрын
Easily transferable to 3d. Replace the sprite renderer with a mesh renderer and replace the y axis for z 😀 Welcome aboard buddy 👍
Hi I'm just getting started with Unity, I'm sure I've messed up something simple. Whenever I call the Tile.Init(Bool) function all of the tiles are positioned in the one spot and the color doesn't change. Any help would be appreciated.
hello very clean tutorial and thanks for the work. I have a problem, when i instantiate the cam the grid doesn't snap on the center of the cam but in high right zone. I tried to give the center point and the cam is in the center but not the grid.
Can this work with Hexagon tiles? Your video is very helpful
Watching this at 50% speed but wish you could pause 2-3 seconds after you swapping the screen, so I have time stopping the video and finish typing :-) But anyway, your videos are very helpful, so thanks for your effort!
@Tarodev
2 жыл бұрын
I do feel like I swap between windows a bit more than I should. I'll try keep it in mind for my future videos!
instead of centering the camera on the gird i wrote: Instantiate(_tileprefab, new Vector2(x - 4.5f, y - 4.5f), Quaternion.identity); its alot easier and if it doesnt line up properly then you need to play around with" 4.5f"
Great Tutorial! can i use the grid in making a 2d chess game or a snake and ladder game in unity...
Hey, so this is a great tutorial and I was wondering if you could clarify the _renderer.color = isOffset ? _offsetColor : _baseColor; Is that just an if/else statement written in a single line? _renderer.color = (if ifOffset is True, _offsetColor, else _baseColor)?
@kordeyrow
Жыл бұрын
Yes, it's a ternary operator
Hey! Thanks for the tutorial! How would you do this method with a flat-top hexagonal grid?
@Tarodev
3 ай бұрын
Checkout my more recent grids video, which shows how to position tiles for all types of grids. You can use the position logic of that video with the game play logic of this one
For me, the tiles are being instantiated 1 pixel apart rather than the size of the tile, which in my case is 75. "var spawnedTile = Instantiate(tilePrefab, new Vector2(x, y), Quaternion.identity);"
Great video! How which variables would I need to change if say I wanted a more condensed grid, so instead of 16x9 squares on screen at a time I want 32x18?
@Tarodev
2 жыл бұрын
Change the width and height of the grid and then zoom your orthographic size on your camera 😊
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
For some reason, the board alternates colors for two columns then defaults to light color. Need some help.
hey, i am having trouble trying to shrink it down. i want to make it all 10x smaller, so x=1 y=1 is now x=0.1 y=0.1 do you know how i could do this?
Great video! I'm following along, but for some reason the Init function is making all the tiles not visible to the camera. I'm not sure how to fix this. I'm on 2021.2.7f1 if that matters.
@againstallevens8512
Жыл бұрын
Make sure your alpha on the color is set to 255 rather than 0.
What would I need to do to have more than one tilegrid within one scene? I'm trying to create two boards for 2 different player to use. Each Player basicly needs their own board at different positions within the same scene.
When instantiating your dictionary it may be good practice to pass in the capacity (width * height in this case) of it as to not consume more memory than necessary whenever the dictionary needs to resize its capacity as you add more tiles. It may be also be worth to consider using a 2d array if you are nit picky about performance and memory as you don't have to save a key for each tile. I would only suggest this if you plan to make a giant array however, may not be necessary or even optimal for small projects.
@Tarodev
2 жыл бұрын
Good tips. I avoid 2d Arrays like the plague and opt for more user-friendly devices. Unless performance is an issue, as you said.
Is it better to use the native Unity tile sistem or a custom tile? I tried using the Unity's tile but it really doesnt like when i try to customize it.
You can use Manhattan distance for the checkboard pattern: (x+y)%2
@playboyy21
2 жыл бұрын
Yo checkout this template for a grid tactics game , this one is by far the best ive seen so far kzread.info/dash/bejne/mqV9o7aJZaWykbQ.html
Nice tutorial, thank you :) However, I have face the problem in the highlight section as the highlight color doesn't show up at all when I place my mouse to the tiles. Do you have any advice to fix it? Ps: My tiles are "Sprite Image" instead of "Color" and it seems like my Unity doesn't have the option "2D -> Sprite -> S̶q̶u̶a̶r̶e̶" for some reason...
@Tarodev
2 жыл бұрын
Do you have the sprite 2D package installed I'm package manager? Make sure your highlight alpha is not sitting at 0. Make sure the sorting order is higher than your tile.
@Ukysseus
2 жыл бұрын
I have the exact same problem, did you find the solution? When my mouse pass over any tiles, the original tile lights up
@jaylonreidhead157
2 жыл бұрын
I finally figured it out!!! I had to replace the Tile object in my assets with the Tile object (with highlight stacked on it) from my scene. That way the script takes my uodated Tile with the highlight asset every time it makes a new block.
Would it be possible to randomly call a Vector2 instead of just the one at current position from the dictionary made at the end?
Feel like I'm kind of falling at the first hurdle here, I don't have the option to make a 2D object. I thought it might be something about Unity's weird multiple render pipelines but no matter if I use URP or specific 2D, nope, still no 2D objects?
Aren't these 'OnMouse' methods actually a raycast? I guess that for huge grids it would be more performant to create a single raycast from the mouse position and check what's colliding instead of a script for each tile. Anyways, I love your content. I'm recomending your videos to a noob friend of mine who is starting with Unity. He's super motivated thanks to your videos! :)
@Tarodev
2 жыл бұрын
Thanks for the recommendation ;)
Hello, Just wondering how the GetTileAtPosition function is supposed to be used? Every way I have tried to use it, it has returned a null value and never a tile.
how can you make sure that you can see all tiles when handling multiple resolutions?
lol this is the first tutorial I see naming a spwned object so it never occured to me that was possible
Hey! Is this system incompatible with the inbuilt Tilemap/Grid system of unity? Fx. You're making a custom Tile class, while there already exists one in the Tilemap package of unity.
@Tarodev
2 жыл бұрын
Tile maps are great, but they're certainly not suitable for most grid games. For example, you can't attach unique logic to a certain tile (easily) using tile maps. One example would be if you had one tile which was a pressure plate which opened a door on another tile... That's very difficult to do using the tile map system.
@sealer1675
2 жыл бұрын
@@Tarodev i see, I was thinking wether or not it would be possible to have both a unity tilemap and a custom grid on top - with unity tilemap used just for tiles and a custom grid for logic
What can i use to make the tile be next to each other if the scale is
How did you get that script editor?
Hi everyone and Tarodev, I love to use events like OnMouseEnter etc. but can you tell me if there is any performance difference between onmouseenter-exit and raycast system? I love to use both of them. Which one should we choose?
@batuhanterzi407
Жыл бұрын
Hi everyone again, if there is anyone seeking an answer for this subject, I chatted with chatGBT and it's saying if there are so many objects with an onMouseDown-Enter-Exit type event on it, using Raycast is more efficient.
Hey for the offset you could have done "(x + y) % 2 == 0"
I'm unable to set X and Y dimension for the GridManager script when I attached to the empty Gridmanager object. As well as unable to add the Tile Prefab to it.
hey guys, really new to coding in general. How does he autofill a lot of his lines? I have visual code studio and it doesn't fill anything in for me as im following along. Is there something else I need to install for coding in unity?
is it possible to use a tile set this way tell it to grab certain tiles from a tile set to do a random generated grid map
@Tarodev
2 жыл бұрын
You can add tile variance as you loop through and set the tile. Add specific tile types with certain weights and roll a dice to determine what to place. If this doesn't make sense come into discord and we'll chat.
i made [SerializeField] private Tile tileprefab; but it is saying tile is a type/namespace that cant be found, please help🙏🙏
Great video! I followed the whole thing and duplicated it perfectly on my end. However, when I run it, I find that the tiles are not visible to the camera. The green colors do not show up. However, I can tell that the core functionality still works because the highlight comes up as I move my mouse around. I'm using Unit 2020.3.29, maybe that's why? :) Thanks for the tutorial!
@matthewjames7513
2 жыл бұрын
Nevermind! I figured it out from a previous comment of yours. For those that don't know this is what you need to do: 1) Click on "Tile" so that the inspector tab on the right shows up 2) Change the alpha of the Base Color and Offset Color 2a) When you click on the color a separate screen comes up where you can choose the RGBA values. Move the slider from 0 to 255.
@gamingcodingwithrandoms1187
Жыл бұрын
@@matthewjames7513 thank you
@garvisan6540
Жыл бұрын
@@matthewjames7513 thank you so much
@dustinwallace9301
Жыл бұрын
@@matthewjames7513 Had the same issue. Thanks for clearing it up!
@birdblue6927
Жыл бұрын
Thanks a lot!!! 👍👍👍👍
In the end of section 3 of the vid my code does not have the options in the grid manager script in the inspector. its my first time coding, did I miss something? SerializeField does not auto complete or change color is this related?
What are the different ways to highlight tiles in Unity? What if I need to cast a shadow under object I am going to put? How to highlight tiles that are already occupied and those that are free?
Would you mind explaining why you prefered to self-create a Grid system instead of using the actual Unity Grid and Tilemap system? I'll be really glad if you do so because I've already seen another youTuber's take on Grid as well (CodeMonkey's) and both of you decide to create your own versions instead of using Unity's. Is there a reason for that? Thanks in advance!
@Tarodev
2 жыл бұрын
Just answered your question in discord! ❤️
do you know how to apply this grid to a custom tileset?
Can somebody help me? I've done everything the master said in the video character to character. After adding the code to change the color of the tiles, the tiles stopped appearing and yes my sprite renderer was not turned off.
hey man how do i access the tile position
Is there a way for the spawned in tiles to be stored into a empty game object (like a folder) to like organize the tiles?
@franciscooteiza
10 ай бұрын
Maybe you can use Scriptable Objects
How do you make GenerateGrid exist ij the first place because my unity keeps saying it does not exist in current context
Hi, great tutorial. I'm a bit stuck around the 5:30 mark changing the sprite colors. I have everything identical to the video, but when I play - my sprites are fully transparent. I can't figure out why - do you know?
@Tarodev
Жыл бұрын
Check that the colors have alpha all the way up. By default it's set to 0
@colbywinfield
Жыл бұрын
@@Tarodev Oh man, duh! I should've seen that - thank you so much! Loving this series, I just subscribed :)
No using Unity(A great engine btw), but this was very helpfull too.