Duplicate a gameobject in right way Unity 5 - unity3d

I have a game object named Bolt with for example white texture , when I duplicate that and rename duplicate into EnemyBolt and change texture to orange the Bolt texture is changing into orange too , how can I copy or duplicate a gameObject that dosen't change its reference?

When you duplicate a GameObject, you also duplicate all the components that are attached to it. This way the new object gets its own set of component instances. This is in Hierarchy panel representing the content of the scene.
The Project panel represents the assets to be used in the different scenes. When you drag a material to a Renderer, that renderer instance points to a material that will be instantiated at runtime.
In order to optimise rendering process, if a material is shared among many object, only one material is created, this will reduce draw calls.
Now for your solution, you need to duplicate your material and assign the new one to the new object. Now, when you change it on one side, it does not affect the other since they are using different material. On the other hand, you increased the draw call.

Related

Does TextureAtlas sprite indices reflect the order in which they were added?

I'm using TextureAtlasBuilder to produce a TextureAtlas. I'm adding multiple textures to the atlas using the add_texture method. I'm then using that texture atlas as part of a sprite entity created from a SpriteSheetComponents bundle.
When changing the sprite's index, the resulting texture is not what I'm expecting. I'm assuming that the textures in the texture atlas reflect the order in which they were added while building it. Is that an incorrect assumption?
The order in which you call add_texture does not guarantee the order in which they are stored in the textures Vec. This is because the current implementation (as of bevy 0.3.1) uses a HashMap to store the placements of the rectangles, thus not preserving the order of insertion.
Since the project is in its infancy, you might consider creating an issue in the project to have this changed.
For the time being, you could either try combining the sprites outside of Bevy and then reading in the TextureAtlas directly from that asset like in the sprite sheet example.

Spritekit: How to limit player movement to certain tiles only

Background of the problem:
As shown in the screenshot, I have a 24*24 tilemap and a player sprite node. The current problem I am trying to solve is, I hope to restrict the movement of the player just within the letter A shaped red tiles.
The solution I have tried:
I used intersects method to detect collision between the tilemap and the player sprite node by frames. However, I hope to compare the player sprite node with only the frame of the letter A shaped red tiles instead of the entire tilemap.
func detectMapCollision(){
if player.frame.intersects(self.mainTileMap.frame){
print("you are on the map! yay! ")
}
}
So, how can I separate the letter A shaped red tiles from the entire tilemap, then compare its frames with the player so I can restrict the player movement to the letter area only?
The way to do this would be to define a tile group and corresponding tile definitions for your game's tiles. Doing this allows you to define specific identifiers in the userData info of each of your tile groups so that you can differentiate them in code.
https://developer.apple.com/documentation/spritekit/sktiledefinition/1645813-userdata
In your game, for example, you could create a tile group that corresponds to all those red tiles that make up the letter A. You can then set a custom key in the userData dictionary for that tile type to identify it. Then check for the key or the key's value in code whenever the Player wants to move to a new tile. You can directly set a tile's userData dictionary in the Attributes Inspector in the variant's tile definition.
The code to check would be something like this (assuming you set a Boolean key 'letterA' to true in the userData dictionary:
let letterTile = myTileMap.tileDefinition(atColumn: column, row: row)
if letterTile.userData?.value(forKey: "letterA") == true {
// Allow the player to move to this tile as it's one of the letter A tiles.
}
Edit:
In response to your query, you do not have to populate your game map/grid in code. To do this I am assuming you have created a game project with a GameScene.sks already prepopulated. In this file you can drop a 'Tile Map Node' from the object library, and that's where you'll use the editor to add your tiles.
Now the tiles with their User Data will come from a SpriteKit TileSet resource file that should also have been added to your project. As of the current Xcode (v11.3.1) the default tile set comes with prebuilt tiles, but there is nothing that stops you from adding your own custom tiles (red tiles for your letter A route). Then when you have created your tile groups and tile definitions, you can modify their user data dictionary by selecting particular variants and adding keys/values in the inspector window.
If you are unsure about how to do the process I have outlined in the previous 2 paragraphs, there are various current SpriteKit TileMap tutorials that are freely available online. I believe they may help you further in this regard.
What must be coded however, is the actual logic to check the UserData info in specific tiles, so that you can restrict player movement as you wish -- an example of which I gave in my original response.

How can i draw an object before its parent object?

How can i change the sorting layer of objects in Unity 2D? Like force an object be rendered above (or under) another object even if it is a child of the other object.
I'm making a simple 2D humanoid animation, intuitively i'm using the torso as the root object, and the arms and legs be the child of it. But it results that my left arms is rendered after the torso, so it looks like my character have 2 right arms.
I tried to change the Z value of my arm, change the layer from UI to others, and tried to change some settings in my Canvas. But none of it worked.
I'm using RawImage to render my character, could that be the problem?
Have you tried using the sorting layers? Unity allows defining sorting layers for 2d sprites. You can select a sorting layer in the sprite renderer component,
You can create and order them under: Under "Tags and layers"
You can also order sprites on the same layer by changing the "order in layer" value in the sprite renderer component.

Unity all objects have same position

I have a 3d building model in my Unity project. It has many children like doors, walls etc. The problem is, all of the children points to same position in the Unity world (24.97, -2.08, 19.35). Their transforms show this position. And this position is far away from their actual one. How can i fix this?
I tried freeing all children from parent but this didn't change anything.
I want them to show their real position, which appears with move tool when we click upon them.
Here is the image link
It seems that this is simply their pivot point exported "wrongly" from the 3D editor your model was made with.
This won't change until you export it correctly from a 3D editor (Blender, Maya, etc).
Unity is not made for 3D mesh modeling and therefore in Unity itself you can't change the pivot points.
There is a very simple fix
Add a new empty GameObject
In the Inspector go to the Transform component, click on the context menu and hit Reset (you also simply set it to position 0,0,0 rotation 0,0,0 and scale 1,1,1) assuming the pivot should be at 0,0,0
Now drag and drop all objects into the empty GameObject
=> You have now one parent object with correct pivot.
By wrapping it in a parent object the childrens pivots don't matter anymore. You can simply do all translation, rotation and scaling on the parent object with the correct pivot and don't have to care about that "wrong" position at all.

Making multiple objects with the same shader fade at different times

I have a death transformation for one of my GameObjects which goes from a spherical ball to a bunch of small individual blocks. Each of these blocks I want to fade at different times but since they all use the same shader I cannot seem to figure out how to make all of them not fade out at the same time.
This first picture is the Spherical Ball in its first step for when it turns from a spherical ball to a Minecraft'ish looking block ball and to the right of it is one of the blocks that make up the Minecraft'ish looking ball shown by the red arrow.
Now this is my Inspector for one of the little blocks that make up the Minecraft'ish looking ball.
I have an arrow pointing to what makes the object fade but that is globally across all of the blocks since they use the same shader. Is it possible to have each block fade separately or am I stuck and need to find a new disappear act for the little block dudes?
You need to modify the material property by script at runtime, and you need to do it through the Renderer.material property. When you access Renderer.material, Unity will automatically create a copy of the material for you that is handled separately -- including getting its own draw call, if you care about performance. You can tell this has happened because the material name in the renderer will change to "Materialname (Instance)".
Set the material's fade property using Renderer.material.SetFloat() (or whatever the appropriate Set... function is). Unfortunately the property's name isn't "Fade Factor". You can find the property's name by looking at the shader script, or by switching the inspector to debug mode and digging through the Saved Properties array for one that looks right.