Grid System Isn't Correct - Error in the course?

The grid system alignment changes between videos. I’ve drawn a box showing a grid position, you can see that the grid position numbers are meant to be in the centre but they clearly aren’t. This isn’t a problem with my code, it’s how it is in the videos as well.

early in the grid system videos

in the later videos

Screenshot 2024-02-09 at 19.46.05

to provide i’m not going crazy, i’ve written some code to draw the gridlines and better demonstrate what I mean

You can see my code block here

It is in fact being placed in the center of the cell.
image
I’ve highlighted the cell in your image here. The grid position is the center of the cell. This is why we used (or edited) the shader to shift the grid on the floor texture 0.5 units. The grid on the floor represents the grid in the grid system

Thanks for the response bizarrio

Please see the video at Grid System | GameDev.tv grid system @ 6:39 and you’ll see the grid is like black line, not the red.

But it’s not. The lines are in the middle of the red square.

The math to determine the cell (Mathf.RoundToInt(...)) puts the grid position at the center of the cell. If the grid position is (0,0) and the cell size is 2, the cell goes from (-1,-1) to (1,1). The game object may sit at (0,0), but that is the center of the cell.

Mathf.FloorToInt(...) will put the bottom-left corner of the cell at (0,0) and Mathf.CeilToInt(...) will put the top-right corner of the cell at (0,0). But we’re using Mathf.RoundToInt(...) which puts it nicely in the center

I remember wondering why he used RountToInt instead of FloorToInt, there was even some questions in the forum about this, because it’s never mentioned that the visualisation is in the centre. It also never mentions that the world position starts at -1, infact he talks about it being at 0 in the same video.

I’ve also watched all the code monkey youtube videos on the grid system and the debug lines he draws in those is never in the centre, it’s always the border of the cell.

I would love to be wrong about this, it would make a difficult subject a bit easier to swallow but he never mentions any of this and I can’t see the logic in starting from world position -1.

The grid is at 0,0. The cells are centered and placed at 0,0. But the extents of the cell goes from -1,-1 to 1,1. Wherever you place the cell, its extents will be from x-1,y-1 to x+1,y+1 (if our cell size = 2)

Mathf.RoundToInt(...) rounds the world position to grid position. 0.34 rounds to 0. -0.34 also rounds to 0. Any value within the extents of the cell will translate to the center of the cell (if you include the cell size in the calculation)

If he used Mathf.FloorToInt(...) you would’ve had to offset all the Units (and anything else that goes in the center of the cell) by cellSize / 2f to get them in the middle.

If you check The Unit Setup lecture, at around 5:30 onward, Hugo discusses how this asset is modified to put the center in the centers of the box and the boxes to be the border.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms