Monster shadow

VIDEO

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

Description:
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: https://pastebin.com/cip74C4E Tile.cs: https://pastebin.com/UD6zE467 ========= ❤️ Become a Tarobro on Patreon: https://www.patreon.com/tarodev ========= 🔔 SUBSCRIBE: https://bit.ly/3eqG1Z6 🗨️ DISCORD: https://discord.gg/GqeHHnhHpz ✅ MORE TUTORIALS: https://www.youtube.com/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
ADD A COMMENT

Aku
Hey there, I'm new on unity programming and I was trying to follow the turotial but for some reason the "Init" in 5:07 gives me an error... I copied everything from the code correctly, I don't know what could be wrong...
MeGaDroiDx
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
Good tips. I avoid 2d Arrays like the plague and opt for more user-friendly devices. Unless performance is an issue, as you said.
Sydney 8551
It's clear, with separated parts in the tutorial itself, very good !
TERATOCARCINOMA
im learning a lot, thanks but i cant make the hightlight effect work :c
Tarodev
Make sure the alpha value of the highlight is not set to 0
John Wayne Creech
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.
Mathew Prasad
My grid is messed up copy the format you did and still the problem
Tarodev
What's messed up about it?
There's nothing here
fast, clever video, amazing!
Haadi Ttt
great content. keep it up!
Phil De Bondi
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.
magni319
Doesn't work in Unity 2020.3. I used your pastebin scripts. Tiles are generated, but don't get colored.
Tarodev
@magni319 I know everything sir
magni319
@Tarodev Wow. How did you know that? You're right, alpha was 0.
Tarodev
Make sure your alpha is not set to 0
Sr Dæve
Thanks idiots
Lluís Vinent
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
Thanks for the recommendation ;)
Cyanberry
Me: uses this script Script: *aight time to crash unity* How do I prevent this from happening?
Mathew Prasad
How did you get that script editor?
ABrandon
Idk if this will still get noticed but: I tried copying the codes and did the steps, but I still get error CS1002 at (8,22). ;w;
ABrandon
@Tarodev I managed to fix the highlights but now my problem is that every "Tile" isn't working. Same codes, but the Tile words are errors: (CS0246 The type or namespace name 'Tile' could not be found (are you missing a using directive or an assembly reference?)
Tarodev
Show me the full line. By the way, do you get syntax highlighting, and do errors show up in your editor? If not go to my channel and find the video to fix intellisense. It'll make your life much easier :)
Alberto Alvarado
Great video! straight to the point.
Ben Conaway
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.
roy hopkins
When creating the Grid manager empty object I cannot add the script to it because i get an error stating "Cant add Script" The script don't inherit a native class that can manage a script.
Tarodev
Does GameManager inherit MonoBehaviour?
Denis MInecraft oros
Amazing!TYSM!!!
Gennaro De Santis
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
Easily transferable to 3d. Replace the sprite renderer with a mesh renderer and replace the y axis for z 😀 Welcome aboard buddy 👍
SEE ALL COMMENTS


Transcript:

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