# Controlling resolution in Unity 3D. Beginner's problems

i started working on developing games one month ago. I really like it, although it isnt easy.

The problem i have now, is with display resolution after i build game. So far i have been working with 2D Space Shooter game (getting help from the tutorial), everything is working smoothly. The only thing that i hardly understand is the resolution, aspect ratio thing. I have made a game that looks like this:

I have drawn a gizmos around the allien formation, and written a script with some calculations that everytime formation hits the edge, it goes to the other edge, hits the other edge, and goes to the other edge, and so on and on. In the free aspect movement looks great, but however, then i change the resolution to any other, movement is stuck, or it barely moves.

Now here is my question. What should i do, to make the game look and work smoothly on every resolution like it works on the free aspect display? I tried to change formation height and width and so on, but i assume this is not an option. I will add my formation controller script as well(it contains several other things so i printscreened only the code that helps formation to move):

Thank you for your time, and sorry if you can't understand everything or something is unclear to you, i'm beginner at game development and English is not my first language.

• Couldn't add a code to the question, so i'm adding it here. postimg.org/image/a7d8zwla5 – sideris Nov 6 '16 at 11:16
• Just a note - it appears that you are using the "Learn to Code by Making Games" tutorial on Udemy by Ben Tristem. It's a great course, and they do cover some stuff about exactly this issue, if not in that part of the course, then in one of the other modules - size, scaling, etc. Actually, it's in the Glitch Garden part of the course. – Jesse Williams Nov 7 '16 at 15:27
• – Philipp Dec 13 '16 at 10:48
• Also helpful: Why are most windowed games not resizeable? – Philipp Dec 13 '16 at 10:50

Shoot-em-ups are a genre where the gameplay is heavily affected by the horizontal and vertical size of the screen. There is a reason why the genre had its apex in the era of home consoles and arcade machines where the developers knew exactly what screen resolution they were targeting.

If you want to have the genre on modern PCs, there are several options, but neither is really satisfying.

• Decide to only allow your game to be played in a window with a fixed size or in fullscreen mode with a fixed screen resolution (Unity allows you to do that in the build settings)
• Change your camera size to stretch the playing field to fit the users screen. But the change to the aspect ration might look weird, and when you use 2d assets they will get blurry.
• Simply accept that your game will play differently with different screen resolutions and design the gameplay with that in mind.

Something come from script must be repaired with script, so also resolution. Something come from size must be repaired with size, this means you must use script to reorder the object location and resize your object based in the resolution.

NOTE: THIS SCRIPT WON'T BREAK YOUR GAME, IF HAPPENED JUST REMOVE IT.

I have a portable script ( not affecting other script ) that will make you understand easier (but the script still have weakness though, as you'll find out later), and in the script you must scale your object one by one, here is the script:

/*

The How-To-Use The Script Is Explained In The Script

*/

using UnityEngine;
using System.Collections;

public class mainmenuresolution : MonoBehaviour {

void CalculateResolution(string ObjectName) {

/*change desiredWidth to width resolution you use when making the game*/

float desiredWidth = 1366;

/*change desiredHeight to height resolution you use when making the game*/

float desiredHeight = 768;

float currentWidth = Screen.width;
float currentHeight = Screen.height;
GameObject TheObject = GameObject.Find (ObjectName);
float currentObjectPositionX = TheObject.transform.position.x;
float currentObjectPositionY = TheObject.transform.position.y;
float currentObjectPositionZ = TheObject.transform.position.z;
float currentObjectSizeX = TheObject.transform.localScale.x;
float currentObjectSizeY = TheObject.transform.localScale.y;
float currentObjectSizeZ = TheObject.transform.localScale.z;
float newObjectPositionX = (currentWidth / desiredWidth) * currentObjectPositionX;
float newObjectPositionY = (currentWidth / desiredWidth) * currentObjectPositionY;

/* if you want your object coordinate "z" scaled too , use : (currentWidth / desiredWidth) * currentObjectPositionZ */

float newObjectPositionZ = currentObjectPositionZ;

float newObjectSizeX = (currentWidth / desiredWidth) * currentObjectSizeX;
float NewObjectSizeY = (currentWidth / desiredWidth) * currentObjectSizeY;

/* if you want your object size "z" scaled too , change to : (currentWidth / desiredWidth) * currentObjectSizeZ */

float NewObjectSizeZ = currentObjectSizeZ;

// moving to new coord
TheObject.transform.position = new Vector3(newObjectPositionX,newObjectPositionY,newObjectPositionZ);
// resizing object
TheObject.transform.localScale = new Vector3(newObjectSizeX,NewObjectSizeY,NewObjectSizeZ);

}
// Use this for initialization
void Start () {

/* change " Your Object Name " To The Name Of Your GameObject */

CalculateResolution ("YourObjectName");

}

// Update is called once per frame
void Update () {

}
}

• Thank you very much! Will try it out at the weekend then i will dive in back to my project. Cheers! – sideris Nov 8 '16 at 19:11
• no prob :) because sharing is caring :) – user6668201 Nov 9 '16 at 9:10