C# Normalized - How to?

Hey all,
Just about done with this menu system, but of course this last step is giving me a headache…
I’m thinking I need to use the ‘normalized’ function to finish this up, but unfortunatly I’m not understanding how exactly it’s used.

Unity Refrence: : Didn’t offer this no0b much of an explanation…

3rd Party Refrence: : Makes me think this function may not actually be what I’m looking for…

PROBLEM:

I’ll be using the function on this line of code:

mainCam.rect = new Rect (0f, 1f - .5f, .5f, .5f);

My intentions are for the top left camera to only take up so much of the scene view (as a miniMap or something would), so my thought process is that since the camera can only be set to a value 0 - 1 before it’s no longer viewable on screen then I would go ahead and ‘normalize’ the screen.width and screen.height information so that both are independently contained between values 0 and 1. I figure this would then allow me to use that data to accurately place and resize the camera accordingly dependent on the users resolution settings.

I’m thinking if this is to work my code would end up looking something like this (I KNOW THIS IS WRONG):

mainCam.rect = new Rect (0f, 1f - screen.width(normalized)/.3f, screen.length(normalized)/.3f, screen.width(normalized)/.3f);

Anyway, that’s that…if there’s a chance I could just do this some other way, or you don’t understand anything I just said lol please feel free to inform me :slight_smile:

Also noticing that ‘normalized’ may only work with vector3’s? if so…I’m going the wrong direction altogether. Probably.

Getting closer to answering this myself, this is what I’m using now…I should be able to figure it out from here with guess/check. But if anyone feels bored, take a poke and see if I’m heading in the right direction :slight_smile: This is working as I intended for it to, but there may be a simpler way? Or a way that uses less code?

		void Update()
			{
				//Normalize Data for camera to screen size 
					Vector2 xAxis = new Vector2(Screen.width, 0);
					xAxis.Normalize();
						Debug.Log ("x is set to " + xAxis);
		
					Vector2 yAxis = new Vector2(0, Screen.height);
					yAxis.Normalize();
						Debug.Log ("y is set to " + yAxis);
		
				//Set Camera Size based on user resolution
					mainCam.rect = new Rect (0f, 1f - yAxis.y * .3f, xAxis.x * .3f, yAxis.y * .3f);
}

Simply multiplication. Normalising a number will give it a range between 0 and 1.

So assuming want to specify a resolution independent position which is in the middle of the screen you can try:

0.5f * Screen.width

And if it needs to be on the right, it’s 1.0f * Screen.width

Is this what you’re looking for? all my res independent stuff is just either multiplication or division.

Lets say we want to specify half the width in normalised coordinates and it’s a 640 screen. That’s 0.5 * 640 = 320

Since we already know the width, any multiplication between 0 and 1 of that value is always “normalised” if that’s what you’re asking. It’s really simple stuff. I probably missed your point.

But rect for camera only takes 0…1 so you should just use 0.25 if you want a quarter, 0.5 for half etc. Not sure why you need normalized.

Hmm, may still be on a different page but I’ve since been able to get it working…I do think that I’m going about it a little sideways still though so feel free to take another shot at it, here’s a simplified version of what I’m aiming for:

In the following code all starting values must be between values 0 1. If the values fall outside the specified boundary the camera position / size no longer meets desired needs ((it’s off screen, unviewable, to large, to small, NOT RIGHT)).

mainCam.rect = new Rect (startingAxisX, startingAxisY, startingSizeX, startingSizeY);

In order to locate a position on screen, such as the screen center point, I am unable to use standard methods like:
(screen.size / 2, screen.height / 2)

This method returns values greater than 1. In this example my screen size is set to (480,320) which would return the values of (240, 160). I needed to figure out a way to normalize this data in order to apply it to the camera specs as intended…So I’ve come up with this:

	//FUNCTIONS
		void Awake()
			{
				//Configure Camera Specs
					Vector2 xAxis = new Vector2(Screen.width, 0);
					xAxis.Normalize();
		
					Vector2 yAxis = new Vector2(0, Screen.height); 
					yAxis.Normalize();
		
				//Set Camera Size based on user resolution
					mainCam.rect = new Rect (xAxis.x * .005f,yAxis.y * .85f - yAxis.y * .65f, xAxis.x * .65f, yAxis.y * .65f);
					countrySelection.rect = mainCam.rect;
		       }

the xAxis and yAxis are set to their corresponding screen directions, the data is then normalized so that it is contained between 0 1, then fed to the camera in order to position and size it. Again, this code works…it just seems a bit dirty and I feel someone with more experience could probably do the same thing with less more structured code :frowning:

It’s easy, 0.5 is the center of the screen.

mainCam.rect = new Rect (0.5f, 0.5f, 0.5f, 0.5f)

would be the bottom right quarter of the screen

Yeah, it works lol i’ll leave it at that