How to scale with resolution?

Hi, I’m making a simple 2D Space Invaders game.
My code for drawing the invaders onto the world takes the pixel width of the camera and divides it by how many invaders I want.
I also need to have a consistent space between each Invader, When I increase the screens width the space between the invaders increases (Can’t have that).
So is there a way of having a constant screen size that I can draw to and scale for larger displays?
Cheers

1 Like

Quit working so hard! :slight_smile: Unity handles all this for you (more or less). You should be using an Orthographic camera; then you just set the Size of that camera to how many Unity units you want between the center of the screen and the top (or bottom). See here for a more detailed explanation.

Having done that, Unity will automatically scale everything on your display for the actual resolution of the screen. You just arrange things in Unity units, and don’t worry about how many actual pixels that turns out to be.

1 Like

@JoeStrout Ok that works great for keeping the Invaders in place but when I change the screens width there is a space between the invaders and the left hand side.

Usually game world stuff is designed to fit a particular aspect ratio. If your aspect ratio stays the same, then your game should look identical on any resolution of that aspect ratio due to the mentioned scaling that Unity does automatically.

The Canvas system can adapt to aspect ratio changes for UI elements and things in screen/camera space, but unless you make your whole game in a Canvas, there’s not much you can do for that.

In the past I’ve used Canvas objects to scale and distribute empty objects as reference points to then place world space elements, but it’s not a particularly elegant way to work.

You could always do more math to figure out the scale and layout.

ScreenWidth / NumEnemies = TargetSpriteWidth // portion of screen per invader

TargetSpriteWidth / ActualSpriteWidth = ScaleToApplyToSprite // get scale for sprite to fill that portion

InvaderSprite.transform.localScale = ScaleToApplyToSprite // apply the scale

// now layout the sprites knowing that NumEnemies will fit across the screen

Right. Camera size is defined in terms of the screen height, not the screen width. And in your case, I would think it would be better to have extra space on the left and right rather than have your game cut off on top and bottom.

However, your game should be centered horizontally, i.e., have extra space on both left and right sides. Can you confirm that this is what you see?

@JoeStrout Yes, but when resizing the game it creates a space on the left and right side.

OK, cool. Sounds like it’s working correctly then.

1 Like

Technically this can’t be prevented. You could try to minimize it by adjusting your orthographic size but a solution that works most of the time was to just design for a slightly flexible world space width.

If this does not work for you I’d like to know why… do tell please…