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

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.

Related

SpriteKit sprite position inside node tree

Currently working on a simple 2D game, where I have player character that is split into multiple sprites (head, torso, legs, arms,...).
I have absolute coordinates right in Aseprite (if I take individual sprite and position them I get correct coordinates).
When I put everything into swift and use negative y instead of positive everything gets totally weird.
For example in Aseprite I have coordinates as follow: head (30, 17), torso (30, 24) and legs (28, 35). Everything aligns perfectly.
In SpriteKit I extend SKNode class and put every subsprites inside with just negative number for y. So instead of going up, I draw down. It looks like that coordinates give in pixels are not correct - sprites are off by few pixels. Mostly is off y coordinate but in some cases (character rotation) also x.
How to get from those absolute upper-left coordinates to correct SpriteKit coordinates then?
Ok I figured what was wrong (several things):
your parent class can be SKNode, but children has to have set anchor point at (0,1)
when changing texture of child sprite, always make sure that sprite size is set to new size of texture. If not it will use previous texture size and resize new texture to old size. This introduces additional problems with positioning. So you have to call: child.size = child.texture!.size() (I used force unwrap because I set (new) texture one step before so I'm 100% sure it exists.
when setting new texture set anchor point again (it seems it gets reset when changing texture of child sprite).

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.

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.

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

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

unity5, box collider 2d size doesn't match platform

I created a 1x7 platform. then added a box collider 2D and set the size to 7. you can see in the picture. the platform and box collider are not matching. how can i get it fixed?
X Offset is the easy answer and definitely not a bad one, but maybe not the best one since it could make this problem show up in other areas.
BoxCollider2D by default has its origin at the center of the object ( where the translate tools are showing).
Since the translate tool is at the middle of the platform, the collider should be centered on it as well.
There are several possibilities that I can think of:
The sprites do not have their pivot at the center (If you want it
that way, use Offset to center the box)
The children of the selected object are offset (They should be
shifted over to line up with everything else, unless you need them
this way for some reason)
Create an empty Gameobject - add any sprite objects that you need in there - press the "GameObject" button in the menu and select "Centered on Children" in order for the empty object to be centered properly. This will have to be done each time you add a new object into the bulk. Hope this helps.