Advertisement
Ryoh

DestructibleTilemap

Nov 1st, 2022
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Tilemaps;
  5.  
  6. public class DestructibleTiles : MonoBehaviour
  7. {
  8.     //Make sure you have using UnityEngine.Tilemaps at the top for this script to work
  9.     private Tilemap tilemap;
  10.  
  11.     private void Awake()
  12.     {
  13.         tilemap = GetComponent<Tilemap>();
  14.     }
  15.  
  16.     public void DestroyTileAtPosition(RaycastHit2D hit)
  17.     {        
  18.         var cell = GetTileAtPosition(hit);
  19.         //This disables the tile
  20.         tilemap.SetTile(cell, null);      
  21.     }
  22.    
  23.     private Vector3Int GetTileAtPosition(RaycastHit2D hit)
  24.     {
  25.         Vector3 hitPosition = Vector3.zero;
  26.         hitPosition.x = hit.point.x - 0.01f * hit.normal.x;
  27.         hitPosition.y = hit.point.y - 0.01f * hit.normal.y;
  28.         Vector3Int cell = tilemap.WorldToCell(hitPosition);
  29.         return cell;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement