Hi there! I need a bit of help.
I made a Game Object called “Terrain” and added a TileMap to it called “grass_tilemap”, with a layer named “grass”. The tilesource uses a 512x512 texture, which I split into 64x64 tiles.
I made a function that puts a random tile wherever you click, and it works fine. But I wanted to make it look isometric, so I rotated the TileMap by 45 degrees and scaled the Game Object to X: 1.5 and Y: 0.75.
I figured out how to handle the scale in the function, but the tiles are still showing up too far to the left, probably because of the rotation. I’d love some help getting the placement to work properly with the rotation.
Here’s my code below!
function on_input(self, action_id, action)
-- Check for touch input action
if action_id == hash("touch") and action.pressed then
print("Touch input detected")
local tile_size_x = 64 * 1.5 -- Tile size in pixels (accounting for scale)
local tile_size_y = 64 * 0.75 -- Tile size in pixels (accounting for scale)
local worldx, worldy = screen_to_world(action.x, action.y, 0)
print("World Coordinates in Terrain Editor: ", worldx, worldy)
-- Convert touch position to tile indices (1-based)
local tile_x = math.floor(worldx / tile_size_x) + 1
local tile_y = math.floor(worldy / tile_size_y) + 1
-- Get tilemap bounds to validate coordinates
local x_start, y_start, width, height = tilemap.get_bounds("Terrain#grass_tilemap")
-- Check if tile coordinates are within bounds
if tile_x >= x_start and tile_x < x_start + width and
tile_y >= y_start and tile_y < y_start + height then
-- Generate random tile index (0-63 for 8x8 tiles)
local random_tile = math.random(0, 63)
-- Set the tile on the specified layer (replace "your_layer" with actual layer name)
tilemap.set_tile("Terrain#grass_tilemap", "grass", tile_x, tile_y, random_tile)
end
end
end