Hello,
I created a grid on isometric view (in 2D project):
public static void CreateGrid()
{
Debug.Log("Create Grid");
Node.TypeTerrain type_terrain_node = new Node.TypeTerrain();
//get the world position of the left of the grid
Vector3 world_left = transform.position + (Vector3.left * grid_world_size.x * node_diameter);
for (int y = 0; y < grid_size_y; y++)
{
for (int x = 0; x < grid_size_x; x++)
{
//get the pos of the current created Node
Vector3 worldPoint = world_left + new Vector3(node_diameter * (x+y) + node_radius, node_radius * (-x + y) , 0);
type_terrain_node.Solid = Physics2D.OverlapCircle(worldPoint, node_radius - mini_radius, solid_mask);
grid[x, y] = new Node(worldPoint, type_terrain_node, x, y); //create the new node
}
}
}
Then, i want to get a tile from the array “grid” with a world position (which set in tile when the grid was create). For information, here is the Node class:
using UnityEngine;
using System.Collections;
public class Node : IHeapItem<Node>
{
public Vector3 position;
public int gridX;
public int gridY;
public Node parent;
public int gCost;
public int hCost;
int heap_index;
public struct TypeTerrain
{
public bool Solid;
public bool Walk;
};
public TypeTerrain type_terrain;
public bool water;
public bool waterEnergy;
public bool windEnergy;
public Node(Vector2 _position, TypeTerrain _type_terrain, int _gridX, int _gridY)
{
position = _position;
type_terrain = _type_terrain;
gridX = _gridX;
gridY = _gridY;
}
public int fCost
{
get
{
return gCost + hCost;
}
}
public int HeapIndex
{
get
{
return heap_index;
}
set
{
heap_index = value;
}
}
public int CompareTo(Node node_to_compare)
{
int compare = fCost.CompareTo(node_to_compare.fCost);
if (compare == 0)
{
compare = hCost.CompareTo(node_to_compare.hCost);
}
return -compare;
}
}
I found this method which works pretty well in orthogonal view (or carthesian) but not in this isometric view:
public static Node NodeFromWorldPoint(Vector3 worldPosition) //Give the grid position of a world position
{
float percentX = (worldPosition.x + grid_world_size.x / 2) / grid_world_size.x;
float percentY = (worldPosition.y + grid_world_size.y / 2) / grid_world_size.y;
percentX = Mathf.Clamp01(percentX);
percentY = Mathf.Clamp01(percentY);
int x = Mathf.RoundToInt((grid_size_x - 1) * percentX);
int y = Mathf.RoundToInt((grid_size_y - 1) * percentY);
return grid[x, y];
}
I was think, it’s a common problem but i don’t find some topic talk about it.
Thanks for your help and your time