Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to destroy tiles in tilemaps?

Discussion in '2D' started by gagazor22, Nov 20, 2019.

  1. gagazor22

    gagazor22

    Joined:
    Nov 20, 2019
    Posts:
    5
    I know the command to destroy but it is not just like I want.
    I made some breaking sprites that show how many durability of the tile is left. Like if it is small breaking sprite, you need to click 2 more times to break it. Or if it is big you need to click only 1 more time to break it. But when I try it, it is just not like I wanted.
    I made a int that would count the times you click, and if it is 4, it would set the tile to null. But, it is for all of the tiles. I want to make the break count for each of the tile. Like, if you click a tile once, I want the small breaking sprite to stay there, and if you click another tile, I want it to break.
    Pleae help
     
  2. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    You don't need to spam people please help.
    I didn't really understand. Do you mean like in minecraft?
     
  3. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @gagazor22

    It is not polite to spam several threads asking for help...

    But to your question - you can't store such data in Tilemap itself.

    You can however create a matching table of positions (like a dictionary or a 2D array perhaps) that stores your classes containing information about each tile of your tilemap.

    When you do digging or whatever it is, just check the position of hit, get tile data for that tile, and reduce some of its value you need to reduce. When tile value goes to zero, remove the visual tile from your Tilemap (it only serves as visual representation of your data).

    You can of course alter the sprite of a tile at any given value... like have a breaking sequence (health is 3, show semi broken cracked block of stone).

    See Unity's tilemap extras I think they had something similar.
    https://github.com/Unity-Technologies/2d-extras
     
  5. aybarsmete1

    aybarsmete1

    Joined:
    Oct 26, 2019
    Posts:
    43
    I believe you can use prefab brush. Just make the same script for a cube, then make it prefab, then assign the sprites for it then break.
     
    gagazor22 likes this.
  6. gagazor22

    gagazor22

    Joined:
    Nov 20, 2019
    Posts:
    5
    Thanks, it works, however, not like I wanted.
     
  7. gagazor22

    gagazor22

    Joined:
    Nov 20, 2019
    Posts:
    5
    Hey, I am not really good at codes, can you create a small sample for me? So that I can figure out what is happening and fit it to mine.
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  9. gagazor22

    gagazor22

    Joined:
    Nov 20, 2019
    Posts:
    5
  10. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @gagazor22

    Begging for someone else to do your work, or code begging is not part of this forum.

    You'll have to do your own learning - everybody has to. There is no shortcut... If you then have questions while creating your code - then ask, otherwise this is getting irritating.
     
  11. gagazor22

    gagazor22

    Joined:
    Nov 20, 2019
    Posts:
    5
    Alright sorry, I think I am figuring things out a little bit. How do I change my mouse position into a grid position?
     
  12. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116