The question is answered, right answer was accepted
Getting World Space From Custom Grid
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.
Answer by Glurth · May 05, 2016 at 03:44 PM
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. (http://docs.unity3d.com/Manual/CameraRays.html)
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. (http://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html)
Thank you for your response, in the start I tried to go for the first option. But because this has to be a quick proof of concept, I've gone easy way and used RayCast.
$$anonymous$$ight should have mentioned it were in a 2D space as well, but it works now, thanks :) Regards