- Home /
My canvas keep being off center and I have trouble instantiating objects at (0,0,0)
Hello. I am learning to instantiate an object, and I tried making a "Card" in the center of the screen, or (0,0,0). But when I play, the object is instantiated at the bottom left of the screen. I opened the editor to find out that the canvas was positioned automatically like this:
The Main camera was positioned at 0, which is the bottom left corner of the Canvas.
I looked around on the internet and learned that I should keep the canvas render mode to Screen Space - Overlay. But this way I can't instantiate my object. Does it have something to do with my screen or editor layout? Can anybody please help me?
Answer by ducthangdang1502 · Jan 06, 2021 at 11:10 AM
Hello again,
After some further studies, I found the answer to my problem here and I want to add to @sacredgeometry 's one.
Great explanation can be found here https://www.youtube.com/watch?v=at6gUB_-HII.
The Screen space - overlay Canvas and the view of the camera are two separate things. In the overlay canvas, the objects are mapped to the screen by pixel like stickers to a glass window. The Canvas in this case corresponds to the screen ratios, leading to weird x,y ratios if you set Free Aspect on the game panel.
On the other hand, the camera is rendering 3D objects in the actual world space. Objects here are not sticked to the screen but move freely in 3D space. You can modify their Z axis and see how they change, unlike the overlay mode.
The game will render both views on the same screen, though, leading to a little confusion at first. Think of looking through a glass window where you can see both the stickers on it (overlay), and the objects outside the window (gameObjects).
In this case I chose to display my card in the overlay layer. I will create the card and then position them afterwards. My approach was to create some GameObject at the place I want my cards to be, and call them positions.
First, I Instantiate my cards without the position parameter. Then, I will snap the cards to the premade positions by setting the the cards position to the premade positions.
Here is the code that will snap the cards to position after Instantiating them:
void SnapCardInPlace() { cardClone.transform.position = cardPosition.transform.position; }
I hope this help other people new to the concept. Cheers
Answer by sacredgeometry · Jan 06, 2021 at 08:03 AM
0,0 in screen space is different to 0,0 in unitys local or world coordinate space. World space origin is in the centre of the the coordinate space, and screen space origin is (depending on the library) one of the corners.