- Home /
How can you position a gameobject in the world aligned to the screen edge?
I am building a simple little game that takes place entirely within one screen, however due to the numerous possibilities for resolutions, I can't just put one object in a place and count on it to stay there. This is not for a GUI - this is for an actual gameobject. The gameobject is dynamically generated and placed at the top left screen corner... at least, that's what should happen. So far I haven't figured out how to place it exactly at top-left viewport.
void buildGround () {
GameObject groundBase = new GameObject("Background Ground");
for (int y = 0; y < gridY; y++) {
for (int x = 0; x < gridX; x++) {
Vector3 pos = new Vector3(x, -y, 0) * spacing;
Quaternion rot = new Quaternion(0,90,90,0);
GameObject groundpiece = Instantiate(groundBackground, pos, rot) as GameObject;
groundpiece.transform.parent = groundBase.transform;
}
}
Vector3 screenPoint = Camera.main.ViewportToScreenPoint(new Vector3(0,0,0));
groundBase.transform.position = screenPoint;
}
The problem I'm trying to deal with is that while the screen may be relatively static, the edge won't always be in the same place.
Answer by Benproductions1 · Aug 22, 2013 at 02:24 AM
Hello,
I'm going to ignore every piece of your code, except for the last 2 lines (because they are not relevant at all)
What you are currently doing, is converting a Viewport coordinate, where (0, 0) is top left of the cameras viewport on the screen and (1, 1) is the bottom right, to a Screen space coordinate, where (0, 0) is the bottom left of the screen and (Screen.width, Screen.height) is the top right.
What you want is a function that converts a Screen space coordinate to a World space coordinate.
AKA Camera.ScreenToWorldPoint
This function takes a Vector3
, where x
is the x
coordinate on the screen, y
is the y
coordinate on the screen and z
is the distance from the camera to the point.
Using this function we can get a point in world space, which is at the top left hand corner of the screen (0, 0) and any arbitrary distance from the camera:
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 5));
groundBase.transform.position = worldPoint;
Hope this helps,
Benproductions1
Perfect! Now I just have to get it into the exact top-left of the screen..
Screen space coordinates move linearly between:
bottom left (0, 0)
and
top right (Screen.width, Screen.height)
As I said in my answer :)
Got it working with 0, screen.height. Now to figure out how to tile the object across variable screen widths :)
If you have any further questions, post them in another question. If I have answered your question please accept my answer :)
Hi Ben,
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 5));
groundBase.transform.position = worldPoint;
This code does not care to width and height of object so the result I get is not like what I expect. You can see my attached picture
][1]
I has tried to add width and height of the object but nothing changed
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(gameObject.GetComponent<SpriteRenderer>().bounds.x/2, gameObject.GetComponent<SpriteRenderer>().bounds.y/2, 5));
What must I do? Thank you
p/s: should I make a new question?
Well, you probably shouldn't post your question as an answer to someone else's question. The problem is that you added the bounds of the object (which are in world space) to the vector you're converting from screen space to world space. If you want an actual answer, you should post it as a new question :)
Answer by purnaprakash · Jul 27, 2016 at 10:53 AM
hi here is the Solution @lamxung55
Vector3 mPos = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, 0));
mPos.z = 0;
mPos.x += gameObject.GetComponent<SpriteRenderer> ().bounds.size.x / 2;
mPos.y += gameObject.GetComponent<SpriteRenderer> ().bounds.size.y / 2;
transform.position = mPos;
Your answer
![](https://koobas.hobune.stream/wayback/20220613112717im_/https://answers.unity.com/themes/thub/images/avi.jpg)