Hello everyone.
Thank you for taking the time to look through and possibly answer this question.
So I have created a pretty easy custom grid that have weird coordinates, using this code:
public Vector2 GridToWorldSpace(int x, int y)
{
Vector2 vector = Vector2.zero;
if (x == y)
{
vector = new Vector2(0, y * 0.64f);
}
else if (x < y)
{
vector = new Vector2((y - x) * -0.64f, (y + x) * 0.32f);
}
else if(x > y)
{
vector = new Vector2((x - y) * 0.64f, (x + y) * 0.32f);
}
return vector;
}
What I’m seeking help to do is making some kind of function that returns the grid object, when you press on it with the mouse. I believe that it’s quite simple, and I’m just too blind to realise it.
Here example of how it gets to look when it runs. Each terrain block is scaled down to make it easier to see where they split.
Thank you in advance and regards.
There are a few way to do this:
1-Take the mouse input screen coordinate and compute the position of that point of your plane (projection), then loop through all you blocks to see which one occupies that point. (looks like this is the method your trying to use now- I’m not going to be much help with this option. Tough I DO see you do NOT take a change in camera position/angle into account.)
2-A variant of this method is to assign trigger collider component to your tiles, and use RayCast to detect which object is clicked on. (Unity - Manual: Rays from the Camera)
3-The best (IMHO) way to do this allow each block to respond to a click event. This might be a bit trickier to figure out, because you’ll need to learn how to implement IPointerDownHandler and IPointerUpHadler interfaces. (Redirect to... title of new-page)