I've implemented an alternative method for detecting grid positions in an isometric Unity game without using a hitmap. Instead, I'm using a combination of mesh colliders and raycasting. Here's my implementation:
// ... other fields and methods ...
public bool IsOddRowAtPoint(Vector3 point)
{
Ray ray = new Ray(point + Vector3.up * 10, Vector3.down);
RaycastHit hit;
if (meshCollider.Raycast(ray, out hit, 20f))
{
int triangleIndex = hit.triangleIndex * 3;
int vertexIndex = triangles[triangleIndex];
return isOddRow[vertexIndex];
}
return false; // Default value if no hit is detected
}
// ... method to create mesh and set up isOddRow list ...
// ... other fields and methods ...
void TouchCell(Vector3 position)
{
position = transform.InverseTransformPoint(position);
bool isOddRow = isoMesh.IsOddRowAtPoint(position);
IsoCoordinates coordinates = IsoCoordinates.FromPosition(position, isOddRow);
Debug.Log($"Touched at {coordinates.ToString()}, Is Odd Row: {isOddRow}");
}
public static IsoCoordinates FromPosition(Vector3 position, bool isOddRow)
{
float x = position.x / IsoMetrics.cellWidth;
float z = position.z / (IsoMetrics.cellHeight);
int iZ = 0;
if (isOddRow)
{
x -= 0.5f;
z += 0.5f;
iZ = Mathf.FloorToInt((z)) * 2 - 1;
}
else
{
iZ = Mathf.FloorToInt((z)) * 2;
}
int iX = Mathf.FloorToInt(x);
return new IsoCoordinates(iX, iZ);
}
This approach uses a mesh collider and stores whether each triangle in the mesh belongs to an odd or even row. When detecting a grid position, it raycasts against the mesh and checks the isOddRow status of the hit triangle.
What I'm unsure of given my method
How does this method compare performance-wise to using a hitmap?
Are there any potential issues or edge cases I should be aware of?
Is there a more efficient way to store and retrieve the odd/even row information for each cell?
How well would this scale for larger grids?
My Grid Currently
Tags: [unity3d] [isometric] [grid] [game-development] [c#]