Advertisement
Guest User

Untitled

a guest
Sep 21st, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Tilemaps;
  3.  
  4. public class BallController : MonoBehaviour
  5. {
  6.     public float ballSpeed;
  7.     public GameObject tilemapGameObject;
  8.  
  9.     [SerializeField]
  10.     private Rigidbody2D rb;
  11.     [SerializeField]
  12.     private GameController _gameController;
  13.  
  14.     private Vector3 startPosition;
  15.     private Tilemap tilemap;
  16.  
  17.     void Start()
  18.     {
  19.         startPosition = transform.position;
  20.  
  21.         if (tilemapGameObject != null)
  22.         {
  23.             tilemap = tilemapGameObject.GetComponent<Tilemap>();
  24.         }
  25.  
  26.     }
  27.  
  28.     private void OnCollisionEnter2D(Collision2D collision)
  29.     {
  30.         Vector3 hitPosition = Vector3.zero;
  31.  
  32.         if (tilemap != null && tilemapGameObject == collision.gameObject)
  33.         {
  34.             foreach (ContactPoint2D hit in collision.contacts)
  35.             {
  36.                 hitPosition.x = hit.point.x + 0.01f * hit.normal.x;
  37.                 hitPosition.y = hit.point.y - 0.01f * hit.normal.y;
  38.  
  39.                 var position = tilemap.WorldToCell(hitPosition);
  40.  
  41.                 var brick = tilemap.GetTile(position);
  42.  
  43.                 tilemap.SetTile(position, null);
  44.             }
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement