Math in Unity : create a “graphic grid” with Cartesian Plane and Line Renderer (Part II)
As for the second problem, we should calculate some sort of “offset” and draw the lines based on that offset multiplied by the size of the grid.
For example: if we have our dimension of 13, our offset will be, in this case, 160 divided by 13.
Remember: our size is an int, but if it were a float, we need to store the result in a variable with EXPLICIT CAST.
Like this :
In this specific case, the result is 12.307…
But because we cast as int, the result is 12.
If we muliply 12 for the size (13), the result is
12 * 13 = 156
What does it mean?
The offset is simply the DIVIDER of the number 160 (in this case).
When we have the divider, we can multiply it by the size of the grid and find exactly where to draw our lines, and being 8 a divider of 160, like 20, at some point a line will coincide perfectly with our Cartesian plane.
The matematical logic is this :
offset (-12) * 13 = -156 (or 12 * 13 = 156)
This is our starting point from left to right or right to left, nothing change.
This is our starting point from left to right or right to left, nothing changes.
In the second iteration of the loop, the next point will be calculated like this:
-156 + 12 = -144
Ang again in the third iteration
-144 + 12 = -132
At the end, this is the result :
-156, -144, -132, -120, -108, -96, -84, -72, -60, -48, -36, -24, -12,
0
12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156
and stop.
Let’s try in code and see
Obviously this process must also be repeated for the Y, unless the number is identical
We now apply this offset in our for loop
In we press Play
We would always have some margin, but it will be towards the outside and not from the point 0, 0 or 0, 0, 0 in 3D.
And of course we can do some tests by changing the size to see the results
If I try with 28, it works without problems.