How can two GameObjects at different positions in the world space have the same position values under the transform attribute? - unity3d

I have two gameobjects at different positions in the world space. But the position values under the transform attribute shows the same value.One object is a cube and the other one is a chess piece (a prefab).Here is the screenshot of the window with the objects. I'm completely new to unity and it would so helpful if someone could help me sort this out.Thanks.The screenshot the whole window.

If none of them isn't child of other gameObjects ,then maybe pivot of chess gameObject is not in center of your mesh ,check your 3D model of chess and set its pivot to center of gameObject.

Your chess piece is certainly set as child of an object located around (X=3.5, Y=2.0, Z=4.5) coordinates. Unity displays the localPosition of Transform components.

In your print screen you have scaled one of the objects 10 times. Could it be that the anchor of that object is at 0, 0, 0 but the rendered Chess piece is then rendered with offset.
Expand your mesh renderer and see if there are any offset variables

Related

How to align tilemap, grid and sprites to center? (Unity 2D Tilemap)

I am making a game similar to Rimworld and I have issues aligning the tilemap, grid and sprites so that they all work as intended functionally and visually.
The default offset for the tile anchor is 0.5, 0.5, 0 and this works entirely until I drop a sprite in the center of the tile, where it appears to be positioned bottom-left (with the sprite's pivot set to Center).
This is a problem because I'd have to manually set an offset (0.5, 0.5, 0) for every single object I instantiate to match the visuals of the tilemap. I tried setting the tile anchor to 0 but then the tilemap moves while the grid position stays the same which messes up click detection. (if only I could move the grid back by (-0.5, -0.5, 0)?
Is there an elegant and easy solution for all of this?
To get a perfect custom alignment for a tilemap or sprite the easiest solution is to take a empty gameobject as spawner , take the position of that empty spawner object and and then instantiate those tile or other prefab at the exact location where you kept those spawner objects by your choice.
something a friend of mine told me, is that instead of trying to align it to the grid so it always moves in the center, just hold ctrl while using the move tool on the sprite that you are moving. It can get annoying, but it works :)
basicly if you change the sprite pivot for top-left or something different of center should work, for me works perfectly.
I have the same problem with my 2d game. when I want to put the tiles, they place with some offset.
You can change the pivot value in the sprite editor for any sprite ( tiles, character, ...) to a custom pivot like (0,0,0), I suggest center value for the pivot.
You can change Tile Ancher for any Tilemap objects too.
this helps you to put anything to map how you are comfortable.

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.

How Does Unity Assign Pivot Point Location on Script Generated Meshes

I have tried to find any information on how the Unity assigns pivot points to object but all I keep finding is threads on how to move pivot points and that it can't be done. I am creating a 2D game with a background that is randomly created with meshes that are wrapped in empty GameObjects. These objects are organically shaped but they have a property that returns a rectangle that bounds the object so that they can be placed in a way that they are not overlapping. The trouble is that the algorithm assumes that the pivot point is going to be the center of the object. What I would like to know is how does Unity decide where the pivot point will be set to so that I can predict how much I will need to move my mesh inside the parent object so that the pivot point will be in the center of the bounding rectangle.
Possible fix:
Try create the meshes during runtime and see if it always places the pivot points at a certain corner or at least relatively speaking the same location.
If it does that you would know where the pivot point is and could take it into account in your code, if you also know the size of the mesh you spawn.
So I think most general and correct answer that I can come up with is that unity assigns the pivot point to the center of the GameObject that you apply the Mesh to. The local coordinates of the vertices of the mesh depending on how you create them mighht place your mesh so that its logical center is not the same as the that of the empty GameObject that it is attached to. What I did to fix the issue was to make a vector from local point (0,0,0) to the center of bounding rectangle and translate the vertices I use to make my mesh by that vector inverted. It wasn't perfect but by far close enough to ensure that I won't have any overlapping meshes.

Unity2D - uniform positioning of transforms with different sizes

I have a rectangle (sprite) and I need to place different game objects (sprites) inside that rectangle but so they are all "aligned" by their bottoms.
For the life of me, I cannot make it work in Unity.
Say that my box has a height of 5.
I want to place the different size objects so they are all "resting" at the 2.5 y axis inside the box.
Does anyone know how I can do that since transform.position measures from the center of the GameObject?
Thanks!
Don't use transform.position, use RectTransform properites, as they take anchor points into account. In particular you need to set the anchor position for the sprite in the prebab / inspector and then use RectTransform.anchoredPosition to position it.