Why a vector is needed?

Hello had a quick question,

I have a project I’m working on and I’m following a tutorial. The question is…

If I have a Camera and I change its size to fit the resolution of the device its on why must the number below be apart of a vector for it to remain the same number? Example-

public class PixlePerfectCamera : MonoBehaviour
{

public static float pixelsToUnits = 1f;
public static float scale = 1f;

Vector2 nativeResolution = new Vector2(240, 160); // see here how its 240 by 160

void Awake()
{
var camera = GetComponent();

if (camera.orthographic)
{
scale = Screen.height /nativeResolution.y;
pixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f) / pixelsToUnits;

}

so when I run this program I get the size of the camera to be 80.

but when I change the code a bit and remove the vector2 and just make a variable holding the value 160 I get a different result, like this.

public class PixlePerfectCamera : MonoBehaviour
{

public static float pixelsToUnits = 1f;
public static float scale = 1f;

int newSize = 160; // change here

void Awake()
{
var camera = GetComponent();

if (camera.orthographic)
{
scale = Screen.height /newSize; //change here
pixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f) / pixelsToUnits;

}

the new value of the camera is now 96? Why is that? I used the same number that was taken from the vector2 but it gave me a different number? Why is a vector 2 needed please explain. Thank you

You are using an integer for the second calculation which will not give the same results as the float value that vector2 is using. So its not so much that a vector2 is needed its a float that is needed.

Also please use the code formatting on code to make it easier to read.

Thank you, Yes it turns out that it was not a float. But why is he using a vector 2 at all? Why not just simply – float newSize= 160; ?

Maybe he was using the ‘x’ portion somewhere else, or the whole Vector2 somewhere else… or maybe he had thought about using it somewhere else, but didn’t. or maybe he thought it just illustrated his point better. lol.

I don’t know :slight_smile:

1 Like