Firstly, I’ve looked at several different sites and forum posts about similar issues but haven’t found anything that enough sense to help me solve my issue. I’ve been bashing my head on my table trying to figure this out. Any help is appreciated
I am trying to use a mouse position to find a grid location (column / row). Can anyone help me write a few lines of code to make this happen?
The grid starts at world position (0,0) and stretches downwards and out. The maximum number of rows is 61 and the maximum number of columns is also 61. The entire grid is stored in a single dimension array. Indexes 0 through 60 represent row 0, columns 0 - 60. Index 61 would mean row 1, column 0. See photo here: imgur
How is each grid position represented in Unity? A single plane? A complicated mesh? Multiple tiles? Whats the relationship between world space and grid space?
The grid is represented by multiple tiles. To be exact, the grid is an array of 61^2 tiles long with row breaks every 61 tiles. The first tile in the grid is drawn by the world coordinates (0,0), (4,-2), (0,-4), and (-4,-2) as in the photo in my original post.
That seems like a very strange way to set that up… if you just created square tiles you can achieve isometric perspective by positioning the camera, or alternatively rotating each tile by 45 degrees.
If you do this then a standard box collider (either trigger or otherwise) can be used for each tile. You can use the camera functions to get a ray from the mouse position, and then cast this ray to see what tile it hits.
A plane is generally the way this is done. It performs much better than GUITextures and gives you the added advantage of automatic scaling (among other things).
I’m actually not using GUITextures at all. Actually, here’s a screenshot of the game in it’s current state: Link
I may look into what you said in the future but for now I’d like to be able to get the grid tile from mouse location on screen for purposes of making a level editor. I believe linear algebra is required.
Looks cool and all, but you must be drawing something right? I can see pictures … You mentioned tiles… so how are you drawing them …
Not that you can’t do it with the maths, I doubt its very complex… it just seems like a non intuitive way to do it given that you are using tiles.
If you create a single plain at the same “height” as your images then something along the lines of:
Ray ray = mainCamera.ScreenPointToRay(point);
int x, y;
if(Physics.Raycast(ray, out hit, 200.0f)){
x = Mathf.RoundToInt((hit.point.x - xOffset) / ySize + xRounding);
y = Mathf.RoundToInt((hit.point.z - yOffset) / xSizef + yRounding);
}
EDIT: Anyways I’m off to bed. Hopefully you sort it out, although I think you should give consideration to my points (i.e. you must be drawing something, somehow, if its not GUITextures its very likely 2D done in 3D (planes), in which case you can easily get the grid position using ray casts and very basic maths).