- Home /
Developing on multiple resolutions
I have a mobile game in which there are objects that spawn at the top of the screen and float. They spawn at a random position at the top of the screen. I just don't understand how to be able to make this work when there are so many different phone resolutions. How do I make it so that the position of where the objects can spawn scale automatically.
Answer by PlayCreatively · Sep 25, 2016 at 11:40 PM
There are multiple ways and this is one of them: Use Camera.main.ViewportToScreenPoint();
. This basically takes a position on your screen and converts it to world space. The down left corner of your screen is new Vector2(0,0)
and the top right corner is new Vector2(1,1)
and everything else is in between, if you understand.
So if you want to spawn stuff at the top, middle of your screen then just do: transform.position = Camera.main.ViewportToScreenPoint(new Vector2(0.5f, 1)) + possibleOffset;
.
Thanks for the reply! When I run the game with this code in the Start() function :
transform.position = Camera.main.ViewportToScreenPoint(new Vector2(0.5f, 1)) + possibleOffset;
It appears far out from where the camera even is. The cameras world position is 0, 0, -10. The object is appearing at 199.5, 639. Any reason this is happening?
Sorry, I meant to write Camera.main.ViewportToWorldPoint();
. Sorry for taking so long.
You solved my problem but perhaps you would know why my gameobject is not visible when in the Game tab in Unity. It shows up in the scene view (ligning up with the camera correctly) but isn't visible in the Game tab.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Resolution inconsistency on Huawei P20 1 Answer
how to make rail and then make something move on it? 0 Answers
Mobile aspect ratio and scaling - Use 16:10 or 16:9 base for full screen background ? 0 Answers
how to build game for Android phone all resolution? 1 Answer