SpriteKit sprite position inside node tree - swift

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).

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 to find the center in unity 2d?

I want to create 2d game. The game is similar to the Scale Puzzle. I've already created nearly all the functionality. Only the last remains.
.
This is example.
And that's how I draw shapes.
.
I click inside a white square, and after 1 seconds a square is drawn regardless of the position of the ball. (x,y).
Square is created programmatically, and it is added to the parent element "SquaresList" with name New Game Object.
How can i do, so that the violet field becomes larger, and in the middle of the screen.
I made it so that for every 3 clicks, "SquaresList" increases Scale by 0.25f, and get negative position of the ball. Example:
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
This does not work correctly.
What options can be? (intersections, find the max/min point, math formulas) ?
Hi NoName :) Your method
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
would work perfectly if your object does not inherit other objects and there positions. To fix this make sure that you reset the position of all other game objects that you use as containers.

Find the Exact Frame of a rotated SKSpriteNode

I really want to know how to find the EXACT frame of an SKSpriteNode if it is rotated. Currently, the frame of an SKSpriteNode looks like this:
This frame is the rect.frame.
However, this frame includes a lot of empty space due to its zRotation. I don't want this empty space and instead want the frame of exactly the SKSpriteNode.
This is what I want:
How can I achieve this? If you have any idea how to find this 'exact' frame of an SKSpriteNode, I would really like to know. Please use SWIFT.
Thank you
This can be done.
Put a dummy node at the bottom left of your sprite that you know the exact size of. Probably use a perfect square. Anchor it's bottom left to the bottom left of your parent. The parent is the one you want the exact size definition of.
From there, two ways:
Scale the dummy sprite to the size of the sprite you're curious about, and use those measurements to determine where the sprite is and what size it is at any point in time.
Put another dummy sprite at the top right of the parent. In this case you can use the midpoint of your two dummy objects, you don't need to use their edges perfectly. Now you can find the position of these two dummies, at any time, and figure out the size/shape/outline of your sprite in world space units.
Here's way 1 animated

Unity3D localposition relation to parent

I'm trying to position some gameObjects relative to it's parent. I'm using localposition, but the gameObjects are not placed well. If I use local position the parent's width is considered to be 1, right? So if I have a plane unity3d considers it to be a 1X1 square.
I tried to put some models in the local position (1, 1) but they are not placed in the top right corner of the plane...
Do you guys have some thoughts of what might be the problem?
You're incorrect: the parents' width is the x/y/z scale of the parent; local co-ordinates are still measured in world units. If your plane is 100x100 horizontally, and centred at 0,0,0 then you need to put the model to (50,0,50) to be at the corner.

animating object along a path in unity 3d

I need to animate an object along a path. I am doing so by creating an animation like:.
This works great but since my terrain is not flat it will be nice if I don't have to deal with the y component. In other words I want to move an object along the x-axis and z-axis and if there is a small slope increase then increase the object y position. same thing if there is a downward slope. Or maybe I have to create a script where it checks to see if the object is colliding with the terrain. if not then decrease its y position. I don't know if this will work meanwhile animating an object though.
I think the easiest way I can think of to solve this is for you to make the object a rigid body and use collision (probably a mesh or capsule collider if its a player character) to get it to sit on the floor.