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.
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;
.