I’m extremely new to unity & C#. Trying to create a random asset generator for trees & rocks on an isometric tilemap & have had no luck finding help online.
I have an empty world & want the generation to happen on a detail level grid so trees/rocks can be mined/cut in the future.
Any comments would be appreciated.
So, there is a few ways i can think that would work but the basic of basic would be get or write a random number or pseudo random generator, select the cords of a tile run the generator let say it spits out ints between 1 and 100, depending on the value that comes out you place a tile/gameobject that represent that number, ex 1 might a tree, 5 a stone, 6 nothing, etc, then repeat for all tiles and you should have a random/pseudorandom placement of resources
Then how you access resource values depends on how/where you save the resource data
This seems do-able but I don’t understand how to find the co-ordinates of a tilemap in unity. EG: clicking on a square doesn’t show its location
Honestly i have no idea how unity places its tiles when it is isometric, you could check their site should be documents how they are placed, you could try to place tiles with a script ex mouse click place tile at this world coord and see where they end up, or grab the bounds of the tilemap and use celltoworld to see where each cell cord ends up on world position
The tilemap coordinates correspond to what’s defined by its Grid component. You can request the tilemap or grid itself to translate between world-space and grid-space. For example, if you have a GameObject at some point in the world, you can translate that to the equivalent for the grid like so, assuming you’ve got a MonoBehaviour with the Grid and some scene GameObject plugged in from the Inspector:
Vector3Int gridCell = grid.WorldToCell(someGameObjectInScene.transform.position)
More practically, we might want to know which cell the mouse is hovering over:
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var noZ = new Vector3(mousePos .x, mousePos .y); // clear away z from cam
Vector3Int mouseCell = grid.WorldToCell(noZ);
Now, you can easily get the tile at that cell location:
TileBase tile = myTilemap.GetTile(mouseCell);
Debug.Log(tile);
Your grid could be anywhere in relation to worldspace, but I like my grid’s (0, 0) corner to correspond to (0, 0) in world space. Outside that single point, you need to ask the grid to translate for between world and grid space due to the isometric grid layout. For staticly-sized square world, it’s likely a good idea to paint your tilemap starting from cell position (0, 0) to (mapSideLength, mapSideLength).
For example, if we wanted to paint a 100x100 area with the same tile, we could do so like this. Here I show two different ways:
for (int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
myTilemap.SetTile(new Vector3Int(x, y, 0), inspectorGivenTile);
}
}
foreach (Vector2Int cell in new RectInt(0, 0, 100, 100).allPositionsWithin)
{
myTilemap.SetTile((Vector3Int)cell, inspectorGivenTile);
}
For your particular case, instead of painting the same tile in every cell, you would need to create an algorithm to select which kind of tile goes at which particular cell location. Keep in mind, that for map generation, it’s a good idea to use a more performant API (tilemap.SetTiles / SetTilesBlock). Also, you might want to draw to different z-levels or to different tilemaps, depending on what you want to draw on top of what.