How to store Tilemap data efficiently? - unity3d

I'm working on a little game where tilemap gets generated.
This is so far not done in chunks and pretty basic.
Single tiles (rule tiles) of the tilemap can be destroyed by the player.
This is working so far (and took me long enough to figure out!)
Now I am looking for a way of saving tilemap data that allows me to give each tile a specific amount of lifepoints (so that the player needs to hit harder tiles twice oder three times before they get destroyed.
Also upon destruction I want the tiles to be able to drop loot (like gems).
How would you go about this?
I thought about putting gems etc. on a different (visible) tilemap and while destroying the tile of the "ground" tilemap check if for the same position of "gem" tilemap there is an object. If so destroy it and drop item to be picked up by the player.
I'm quite lost on this one, so every input is very welcome!
Thanks.

Related

How can I read through multiple layers of a tilemap to determine what tiles exist at a clicked on position in a script for unity?

Whats going on is, I want to detect what the tile I'm clicking on is, but I'm unsure of how I can do that if my tilemap consists of multiple layers. For example, with the way my script is currently set up, the ground level 'island' can be passed into the script as the 'map' variable, but then I wont be able to see if I am clicking on the house, which is in a separate layer. I'm new to Unity so I apologize if I'm explaining it badly, but basically I need a way to look through multiple layers of the tilemap to see what is being clicked on. In the future I would want to implement some sort of system in which a tile could have some sort of modifier sprite on top of it in a higher layer, so I would want to see the tiles in both layers, another reason I'm wondering if there's a way to cycle through those tiles.

Does changing multiple sprites on a single animation affect performance?

I have been building a 2D sprite based game for which I want to have the player be able to customize their equipment. This means that although I am fine with drawing the content, I'd need to ensure animations in the game run fine on top of each other. For this, I have been preparing a game object with several children to account for the equipment:
Each of the children runs a single animation and should have to follow the player, which I accomplish by using transform.localPosition = Vector2.zero; on the Update of the script each takes, so they hook to the parent's pivot and follow the player. While this has worked for the most part, there are moments in which all of the objects are not synchronized and as such sometimes the parent object (the body) is seen where it shouldn't since the other game objects should render on top:
Aside from that, to make it easy for the children to follow the parent position I had made sprites which are all the same size, which risks me having to load a lot of transparent space per sprite.
Another problem that I just noticed as I'm trying to address the issue of loading too many useless pixels involves the positioning with other objects such as the Sword game object, which doesn't follow the player fully if I use sprites that are not perfect squares (see this question for details How to align sprites of smaller sizes to a moving gameobject sprite? and this one Sibling sprite doesn't appear to follow main GameObject sprite even when transform.position updates)
I tried to fix this by making the Sword a child of the Hero, but even then changing the position through a function that sets values to add on to transform values of the sword game object only change the position of it relative to the initial value. I attempted changing the pivot of the sword sprites to a custom value to guess where the center of it would align with the main game object and appear in the right position, but even that doesn't seem to work.
I'm kind of getting tired with my current process, as I have to rely on several animations for each of the game objects, both parent and children, so that these obey to different layers in a single animator (or in the case of the sword, a separate animator), all to ensure there is some synchronization that doesn't always occur:
I really don't mind the web that is turning out in what I'm doing, but the fact that I have to repeat it across multiple layers with no real guarantee that all the objects would appear right on top of each other due to the fact of having multiple animations playing, and loading multiple sprites with empty space is becoming more of a chore than enjoyment.
So I think I came up with a possible solution: If I could make a single animation for the whole equipment used at any given point (whether only wearing pants or wearing full equipment), then having this single animation could guarantee synchronization across parent and children without the need for animator layers or special functions to update position or worrying about pivots or square sprites if I can set the position of non-square sprites in the animation, with the downside that I would need to account for every single animation for each possible equipment variation (so if I had even 3 of each sword, pants, boots, etc. that would mean 3^6 animations) and make a more complex web of animator states. The only thing I'd be worried about in this case, however, would be the performance, if having too many animations for a player would affect how fast these load. But at the benefit of eliminating the other problems mentioned, my question boils down to this:
Is it better to have a single game object with animations that change multiple sprites across children game objects and a single animator that chooses states based on multiple variables, or game objects with multiple animations that change a single sprite for each, and a single or multiple animators with multiple layers that choose states based on multiple variables?
There isn't really a set answer for something like this. It really just depends on how good your/the players computer is when playing the game. Sorry if this isn't what you wanted.

How can I remove small part of collider of game object in unity 2d

click here for gif show of what I want
I want to remove mesh of object when user click on object and also remove its collider to make another object fall from that removed mesh area...
I am using unity since last month so I don't have much experience and knowledge, please help me...
Creating the destructable ground particles
One way to achieve whats shown in the gif is by creating a prefab of e.g. a circle collider that is instantiated in the area of where the dirt is in your gif. It acts as a "ground particle" and keeps the objects above itself.
You instantiate a lot of them in the area so it acts as a big collider although it is actually a whole array of smaller colliders.
Implementing the interaction logic and deactivating the ground particles
Ater that you implement the functionality of dragging the mouse over the ground particles, removing them. That is also not difficult. Shoot raycasts into the screen at the postition of the mouse (remember to use Camera.ScreenToWorldPoint) and get the collision information (confer to https://docs.unity3d.com/ScriptReference/Collider2D.Raycast.html). With the collision information you can get the reference to the instance of your ground particle(raycasthit.other.gameobject) which is then disabled through script(gameobject.setActive(false)).

Unity 2d: Interactable tiles

I want to create a 2d game with tiles that are interactable, i.e farmland, trees, breakable rocks, etc.
Now if I create my tilemap in Tiled which I have done so far, it does not offer that type of control.
I found some posts on how to deal with this but they are at least 3 years old, so wondering if there is some updated answer? maybe something built in to Unity or a plugin I'm not aware of to help create Tilemaps inside Unity?
I want, for example, 1 tile of grass to be able to react if I hit it with a hoe, or water, and change to dirt. Is my only option to create prefabs and place every tile as its own gameobject? won't that kill performance in a large world?
You could try exporting your Tiled tilemap to .tmx format, creating a script in Unity that loads .tmx files for your map and reads every single tile in a for loop.
You would need to assign a prefab to each tile ID of your tilemap, and in that prefab you could have your script with logic attached (grass reacting to a hoe/water) and graphics.
Other idea is to export one big image of your map, and then export .tmx format in the same way as explained before but create game objects only for ID's that player can interact with and only attach scripts to them.

Breaking up a sprite in cocos2d

I'm making a 2D plane fighting game for the iPhone in cocos2d. I'm trying to make it so when you shoot an enemy plane, it breaks into a couple of separated pieces that will fall out of sight. What is generally the best practice for breaking one sprite up into many? should I create new images for each separate piece, or treat the initial image like a sprite sheet, and make a new sprite from segments?
Please look at this tutorial
It makes a grid of points then moves the internal (not-edge) ones around randomly a bit so it's not all perfect triangles. Then each update it moves/rotates the triangles separately--then draws them all at once.
You treat the whole thing as a sprite, so can run any of the usual actions on it. This example uses CCMoveBy to move the whole group down off the bottom.