- Home /
Trouble with positioning
2D project. I have a GameObject which I use like a container. Rect position is middle-center. While the game is going I add some objects inside this container, hovewer the size doesn't change. It is 0px widht and height. I have choosen another way to put it right in the center - I know how many object inside this container also I know width and heigth of them. I calculate whole width and whole heigth then divide it on 2 and calculate new Vector3 position in this way: int horizontalLinesNum = horizontalLines[0].Count; int verticalLinesNum = verticalLines[0].Count;
float newX = -(horizontalLinesNum*LINE_WIDTH) / 2;
float newY = (verticalLinesNum*LINE_WIDTH) / 2;
Vector3 newPosition = new Vector3 (newX, newY, 0);
this.transform.position = newPosition;
I've checked values in debug. It calculates newX = -162; newY = 162; Container appears, but not in posiotion(-162, 162,0). It appears in position(-188.3721, -353.4884, 0);
HOW COME? I can't understand where it got this numbers. I have stucked with this for a three days. It blows my mind. Guys, please help.
Answer by Tehnique · Mar 30, 2015 at 07:01 AM
It seems there are other things going on that affect your object position after you set it. You can try to set the position in LateUpdate, so it's the last position setting that happens.
I had tried this, hovewer no luck. Function works. It recalculates every time right position, when I use.
this.transform.Translate (newPosition)
Although I don't increment values of position, It is continue moving every frame.
If I use
this.transform.position = newPosition;
It appears on the same position as I've described above.
Is your GameObject parented to another? If it is, the parent object's position will affect positioning. Child position is composed with parent position, so you will get weird results. Also, non-uniform scaling on the object might affect it.
Try:
If you have a parent for your object, set the parent position to 0,0,0 and then check if your object is still moved to a weird position after you move it.
Set your object's scale to 1,1,1
I use a new GUI. So I have a Canvas object on the screen. If I add objects without parent I can't see them, but they have proper coords. Hovewer if I put them inside canvas object they have wrong positions
Is your Canvas Render $$anonymous$$ode set as Screen Space? Also, read this, it might help with understanding how to implement your scenario.
Thank you for the link. I solved it in this way:
I changed the main Canvas to not have the same size on all devices. So,it has scale 1,1,1 now.
After I added a new GameObject at first it was put not in Canvas in position 0,0,0. After I changed parent, coords were changed to new binded to global coords. Now I set localPosition of GameObject and it is ok now.
Now I am able to move these objects properly on my screen!!!
Thank you!
Answer by MigunovAlex · Apr 03, 2015 at 12:35 PM
I changed the main Canvas to not have the same size on all devices. So,it has scale 1,1,1 now.
After I added a new GameObject at first it was put not in Canvas in position 0,0,0. After I changed parent, coords were changed to new binded to global coords. Now I set localPosition of GameObject and it is ok now.
Now I am able to move these objects properly on my screen!!!