I have created a grid (5x7) composed of several small planes (1.6f length and width each) of which I recovered the name (here it will be a number (see in the image below)).
What I’m looking for is to retrieve the row and column of the box that has been clicked.
Here it would be if we click on the box 27, then the code must return the row(3) and the column (6) of the box.
Here is my code (with a cube on top that sends a raycast to the grid):
void Update()
{
Debug.DrawRay(cube.transform.position, Vector3.down * 10, Color.red);
RaycastHit hit;
Ray ray = new Ray(cube.transform.position, Vector3.down);
if (Physics.Raycast(ray, out hit))
{
print(hit.transform.gameObject.name);
}
}
My question is how to get the row and column values of the box that was clicked (here 27).
Here is most explicit :
When you make it, put a little MonoBehaviour on it telling its coordinates. Doesn’t have to be much, just something with public integers for column / row.
Otherwise you can use math to figure it out:
subtract the zero datum (what you consider “center”)
divide each coordinate by the size of the cell (center spacing)
float worldPositionX = ... from the raycast above
worldPositionX -= CenterX; // wherever you consider the "zero" line of the cells
worldPositionX /= CellXSize; // center spacing of cells
int cellX = (int)worldPositionX;
Do that for X and do that for Y. If you’re unclear on the process, feed some values into the above to see it in action.
Although it’s always better to just put a MonoBehaviour on each one:
using UnityEngine;
// @kurtdekker - put one of these on each cell, fill it out with X/Y cell values
//
// when you click on one, use GetComponent<T>() to find this Component, and if
// it exists, use the across/down values
public class CellCoordinates : MonoBehaviour
{
public int across;
public int down;
}
It’s two (2) public integers in a MonoBehaviour. I’m not sure how much simpler it could be. I don’t think there is anything that could be deleted from the above… it is a total of six or even lines of code combined.
for (float i = -Lignes / 2; i < Lignes / 2; i++)
for (float j = -Colonnes / 2; j < Colonnes / 2; j++)
{
CellX = i;
CellY = j;
}
As you can see the values of CellX and CellY can have negative values or numbers to the decimal point.
What I am looking for are positive rows and columns.
Your original post asks how to get row and column.
These are integers, which is why GridCoordinates has integers.
If you want float, then make GridCoordinates be float instead.
I suggest instead that you keep them as integer.
To get integer grid values out of your centered grid, change your loop to:
int i2 = 0;
for (float i = -Lignes / 2; i < Lignes / 2; i++)
{
int j2 = 0;
for (float j = -Colonnes / 2; j < Colonnes / 2; j++)
{
// TODO:
/// add a GridCoordinate to this GameObject
/// use i2 and j2 to set CellX/CellY here
j2++;
}
i2++;
}
This is just super basic iteration and generation, and I’ve given you several examples now.
If you’re not prepared to do this, just put a single GridCoordinate on each one and set it by hand.
Since VS does not recognise GridCoordinates as a type, you probably did not copy this file to your project, did you?
It seems from everything that Kurt said you only took parts of it.
The point of that small component is that you can attach it to every grid cell gameobject in your grid. Each of those components can hold the row and column of that object. You just need to set them when you create the gameobject. That’s what those 3 lines do. So you instantiate a grid cell object, add a GridCoordinates component to it and set CellX and CellY of that object.
When you raycast against your cell objects, you can use GetComponent to get the GridCoordinates component of the cell object that you hit. Now you can simply read out the CellX and CellY values from that component. That was the main idea behind this approach.
btw: you called the instantiated object “obj” Kurt named his variable “copy”. Since you haven’t changed that in your code shows you haven’t even understood what this was about. Please read the answers carefully and don’t rush over details.
I’m sorry but I don’t understand everything you’re telling me.
What I’m looking for is code that returns the row and column of the box that was clicked on.
For exemple, If you click on the 27th box, the code returns CellX= 3 and CellY= 6;
You can see how I don’t care about the physical position of the boxes at all. I only care about their x and y positions in the blockGrid 2d array, and use BlockGridManager.GetGridPosition() to calculate their position. You can also see how each LetterBlock keeps track of its own position.
With a mere few lines of code you could implement the functionality to click the boxes and read their index positions in the grid. You’re free to give it a crack in the project.