I am making a game like terraria but top down, so I made a random generated tiles and on run time whenever the player press on a tile I shoot a ray from player to the mouse and return the tile position, the problem is i can’t detect the tile it always returning the tilemap gameobject not the individual tile position.
this is my code
RaycastHit2D hit = Physics2D.Raycast(transform.position + new Vector3(2,0,0), Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position, Mathf.Infinity);
if (hit.collider != null) {
Vector3 pos = (hit.collider.transform.position);
miningArea_Generator.cave_TileMap.SetTile(miningArea_Generator.cave_TileMap.WorldToCell(pos), null);
}