- Home /
Can not place a sprite programmatically at the bottom left corner
I have just starting playing with unity. Yet, I haven't gotten my mind around the coordinate system. I have read various questions/answer, documents, watched tutorials. When I read docs, world, viewport and screen coordiantes make sense. However when I try to use those, results are completely surprising for me. That makes me think that I am missing something fundamentally important. Would someone help me please ?
Here is what I am trying to do. I am trying to build a game, which will have X number of pieces on a board of size MxN. I would like to place pieces PROGRAMMATICALLY. So, I expect to see a piece on the bottom left corner, when I place a piece on (0,0) screen coordinates. But to my surprise, piece gets placed approximately at the center of the board. I could not figure out why.
Here is my code:
Vector3 BottomLeft = new Vector3 (0, 0, 0);
GameObject token = Prefabs[0];
GameObject go = Instantiate (token, BottomLeft , Quaternion.identity);
SpriteRenderer renderer = go.GetComponent<SpriteRenderer> ();
renderer.sortingLayerName = "Foreground";
renderer.sortingOrder = 0;
And below is output of the code above:
As one can see that piece is placed at the center. I am well aware that I didn't "normalize" the coordinates. Below version of the code converts Screen points to world coordinates:
Vector3 BottomLeft = new Vector3 (0, 0, 0);
GameObject token = Prefabs[0];
GameObject go = Instantiate (token, Camera.main.ScreenToWorldPoint( BottomLeft ), Quaternion.identity);
SpriteRenderer renderer = go.GetComponent<SpriteRenderer> ();
renderer.sortingLayerName = "Foreground";
renderer.sortingOrder = 0;
But the result is still somewhat similar to the first one, although piece is placed slightly to the left and down.
I was expecting the piece to be placed that bottom left corner but it is not. What am I doing wrong ?
Thanks in advance.
Your answer
Follow this Question
Related Questions
Change position of player to create a teleport effect. 2 Answers
Multiple images in GUI SelectionGrid 0 Answers
How to stop camera in a 2D game in a certain coordinate.x? 1 Answer
2D Lighting 0 Answers