How to center texture in second camera?

This is, admittedly, a dumb question, but I can’t figure out which tools to use. OK, so I know how to create a texture (such as a crosshair) and center it in the screen with old school OnGui and resoruces.load. (Thanks to a nice YT video). Works great.

Now, I have created a second camera, culled it to the GUI layer and given it a smaller rect so it’s now a small window on the bottom of the screen. Standard UI camera stuff.

Now for the (lol) hard part: How do I center that crosshair in this camera’s rect? I know how to do it in the main screen; I just want to do the exact same thing for the second camera instead of the screen.


You can get your cameras viewport rectangle from Camera.rect. After that you need to clamp the values so you are only taking into account the part that is being rendered inside your screen. After you have your actual normalized viewport rect you can average the values of x and x+width and y and y+height, do a few adjustments for silly coordinate basis differences between GUI positions and everything else, et voilà! You have the centre of your camera.

public Camera cam;
Vector2 centre;

void Start(){
	centre = CalcCentre(cam);

void OnGUI(){
	GUI.Box(new Rect(centre.x - 50, centre.y - 50, 100, 100), "");

Vector2 CalcCentre(Camera subCam){
	Rect subRect = subCam.rect;
	subRect.width += subRect.x;
	subRect.height += subRect.y;
	subRect.width = Mathf.Clamp01(subRect.width);
	subRect.height = Mathf.Clamp01(subRect.height);
	subRect.x = Mathf.Clamp01(subRect.x);
	subRect.y = Mathf.Clamp01(subRect.y);

	subRect.width -= subRect.x;
	subRect.height -= subRect.y;
	Vector2 subCentre = new Vector2(subRect.x + subRect.width/2, subRect.y + subRect.height/2);
	subCentre.x *= Screen.width;
	subCentre.y *= Screen.height;
	subCentre.y = Screen.height - subCentre.y;
	return subCentre;