void DestroyTile(Collider2D collider, DestructibleTerrain dt) { Debug.Log("DestroyTile -- " + dt.gameObject.name); Tilemap tilemap = dt.GetComponentInParent <Tilemap>(); Vector3Int tilePos = tilemap.WorldToCell(collider.gameObject.transform.position); Debug.Log(tilePos); Debug.Log(tilemap.GetTile(tilePos)); tilemap.SetTile(tilePos, null); }
IEnumerator WallDestruction() { Vector2 pos = transform.position; Vector2 dir = rb2d.velocity.x < 0 ? Vector2.left : Vector2.right; float WallDestHeight = GetComponentInChildren <Collider2D>().bounds.size.y; float WallDestDist = 1; bool tryAgain = false; do { tryAgain = false; RaycastHit2D[] hits = Physics2D.BoxCastAll(pos, new Vector2(1.0f, WallDestHeight), 0, dir, WallDestDist); //Debug.Log("LENGTH: " + hits.Length); foreach (RaycastHit2D hit in hits) { //Debug.Log("HIT: " + hit.collider.name); DestructibleTerrain dt = hit.collider.GetComponentInParent <DestructibleTerrain>(); if (dt == null) { continue; } Tilemap tilemap = dt.GetComponentInParent <Tilemap>(); Vector3Int tilePos = tilemap.WorldToCell(hit.point); if (tilemap.GetTile(tilePos) != null) { tilemap.SetTile(tilePos, null); if (dt.DebrisPrefab != null) { Instantiate(dt.DebrisPrefab, hit.point, Quaternion.identity); } // Because terrain is a single collider, we need to // repeat the cast to see if any other tiles would be hit. tryAgain = true; yield return(null); // wait a frame } } } while (tryAgain); }
IEnumerator WallDestruction() { Vector2 pos = transform.position + (spriteRenderer.flipX ? -AttackPrefabOffset : AttackPrefabOffset); Vector2 dir = spriteRenderer.flipX ? Vector2.left : Vector2.right; bool tryAgain = false; do { tryAgain = false; RaycastHit2D[] hits = Physics2D.BoxCastAll(pos, new Vector2(1.0f, WallDestHeight), 0, dir); //Debug.Log("LENGTH: " + hits.Length); foreach (RaycastHit2D hit in hits) { //Debug.Log("HIT: " + hit.collider.name); DestructibleTerrain dt = hit.collider.GetComponentInParent <DestructibleTerrain>(); if (dt == null) { continue; } Tilemap tilemap = dt.GetComponentInParent <Tilemap>(); Vector3Int tilePos = tilemap.WorldToCell(hit.point); if (tilemap.GetTile(tilePos) != null) { tilemap.SetTile(tilePos, null); // Because terrain is a single collider, we need to // repeat the cast to see if any other tiles would be hit. tryAgain = true; yield return(null); // wait a frame } } } while (tryAgain); }
void DestroyTerrain(DestructibleTerrain dt, Collision2D collision) { if (dt == null) { return; } Tilemap tilemap = dt.GetComponentInParent <Tilemap>(); Vector2 pos = collision.contacts[0].point; Vector3Int tilePos = tilemap.WorldToCell(pos); if (tilemap.GetTile(tilePos) != null) { tilemap.SetTile(tilePos, null); if (dt.DebrisPrefab != null) { Instantiate(dt.DebrisPrefab, pos, Quaternion.identity); } } }