I have tried for a week to make this work, and I figured it is time for some help so I can get unstuck and continue to learn.
I am creating a tank dueling game very similar to "Artillery Duel" from the classic atari era. I found a script to procedurally generate a series of tiles as a terrain. I got it working, but it doesn't actually produce "clones" viewable in the hierarchy. Not sure why but I don't think that detail is important at the moment.
The situation is that I fire a mortar from a cannon. That mortar on its collision creates a new object called "explode" that has a larger translucent sprite and a mask to hide the terrain behind it. So it looks like I destroyed the terrain with an explosion, but I haven't actually yet.
The problem is I want to actually destroy the tiles that have now collided with the newly created "explode" objects collider. This is important so the tanks fall when the impact is under them, or a new mortar doesn't continue to collide with terrain that was already impacted.
I placed an FSM on the "explode" object. I have tried messing around with the actions Get Collision Info, Set Tile, Set Rule Tile, Array List Get Object Inside Collider 2d(which had me messing around with tags but that didn't work either) It is a shame I can't just use Destroy Object in a way that lets me remove certain tiles in the collider, instead of destroying the entire terrain.
I am stumped. If someone could just lay it all out for me so I can see what I am doing wrong, or if I am just completely wrong about my way of going about it....I would be eternally grateful. I attached the terrain generating script and a screen cap of the editor if that helps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TerrainGenerator : MonoBehaviour
{
[SerializeField] int width, height;
[SerializeField] float smoothness;
[SerializeField] float seed;
[SerializeField] TileBase groundTile1;
[SerializeField] Tilemap groundTilemap;
int[,] map;
// Start is called before the first frame update
void Start()
{
seed = Random.Range(0, 100000);
Generation();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Generation();
}
}
void Generation()
{
groundTilemap.ClearAllTiles();
seed = Random.Range(0,100000);
map = GenerateArray(width, height, true);
map = TerrainGeneration(map);
RenderMap(map, groundTilemap, groundTile1);
}
public int[,] GenerateArray(int width, int height, bool empty)
{
int[,] map = new int[width, height];
for (int x = 0; x < width; x++)
{
for (int y = +0; y < height; y++)
{
if (empty)
{
map[x, y] = 0;
}
else
{
map[x, y] = 1;
}
}
}
return map;
}
public int[,] TerrainGeneration(int[,] map)
{
int perlinHeight;
for (int x = 0; x < width; x++)
{
perlinHeight = Mathf.RoundToInt(Mathf.PerlinNoise(x / smoothness, seed) * height);
for(int y = 0; y < perlinHeight; y++)
{
map[x, y] = 1;
}
}
return map;
}
public void RenderMap(int[,] map, Tilemap groundTileMap, TileBase groundTilebase)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (map[x,y] == 1)
{
groundTilemap.SetTile(new Vector3Int(x, y, 0), groundTilebase);
}
}
}
}
}