- Home /
drawings secondary cameras in specifics places
I have the world coordinates and pixel size of a section of the screen that I want to draw a camera viewport over. Think minimap. I hand placed the normalized viewport rect for the designed aspect ration but need to support many different aspect ratios. since the rect for displaying a camera is normalized... a .25 width is different between different aspect ratios.
I cannot, for the life of me figure out how to get a specific world position and pixel height/width to convert to a normalized viewport that works in any aspect ratio. I only really need to adjust the width.
Here is what I have so far... totally doesn't work
Camera miniCamera = GetComponent<Camera>();
Vector3 pos = Camera.main.WorldToScreenPoint(
new Vector3(myPosition.x - (myWidth/2f), 0f, 0f));
miniCamera .pixelRect= new Rect(pos.x, miniCamera.rect.y, imgSprite.width, imgSprite.height);
Answer by TheHats · Jul 04, 2013 at 02:22 AM
Solved it finally. Here is my solution. I got caught up on having to cast the Screen sizes to floats. :(
Camera camera = GetComponent<Camera>();
Vector3 pos = Camera.main.WorldToViewportPoint(
new Vector3(buttonSprite.transform.position.x - (imgSprite.width/2f),
0f, 0f));
float scaledWidth = (Camera.main..orthographicSize*2f) *
((float)Screen.width / (float)Screen.height);
camera.rect = new Rect(
pos.x,
camera.rect.y,
imgSprite.width/scaledWidth,
camera.rect.height);