- Home /
How to resize a camera's orthoSize for an object to fit inside its rect viewport?
Hello, this is another follow-up of this series of questions. So I'm still working on the Examine feature of Resident Evil.
Short summary of my setup once again:
I'm using NGUI, I have a "Examine_UI" UI, which has its main camera (which you can see in the left image below). This camera can only see the UI layer, which is "2D_Examine"
Going down you got "ExamineCamera" which only sees the "ExamineArea" (grey window in the middle of the UI, left image) and its children.
So after I got the weapon to appear only in the desired area, and re-calculating the examine camera viewport every time the examine window changes position, I'm stuck with a problem:
Weapons/objects could have different scales upon examining them, which means, they could either appear big in the examine window (large scale) or small. What I would like, is to have the weapon fit nicely inside the examine window no matter how big or small it was.
I can think of 2 ways of achieving this:
If the gun was bigger/smaller, I would keep increasing/decreasing the size of my orthographic camera, till the gun fits. The problem is I don't know what 'till the gun fits' means! I mean what would that imply? To answer, I'd have to find a connection between: The gun's size and/or scale, the camera's orthoSize and its rect viewport. What is the connection between those?
Instead of changing the camera's size, I could maybe scale down the gun till it fits. Here, I think I might be able to figure out what 'till it fits' means. I could maybe take the bounds of the gun's boxCollider, and compare it with the camera's rect viewport, keep scaling down, till they're about the same width and height. However that requires me to save the gun's previous scale, so that after I'm done examining I take it back to that scale.
I would really like to apply the first method, it sounds more efficient and faster, and don't mess with the scale at all.
So, how can I achieve it? change the cam's orthoSize till the gun fits the window? Is this possible or am I gonna have to go with the 2nd method, which I'm also having trouble with :/ - I just want ideas, thoughts, not code.
Please notice that I only want to do this thing upon the beginning of examining an item.
If you're interested in my current code of examining:
public void ExamineItem(Item item)
{
// if it's the same item, don't do anything
if (item.gameObject == examineInfo.itemObj)
return;
// if there's already an item we're examining, let it go
if (!doneExamining)
Notify_DoneExaminingItem();
doneExamining = false;
// activiting the item object if its disabled
examineInfo.wasActive = item.gameObject.active;
if(!examineInfo.wasActive)
item.gameObject.SetActive(true);
// store item's layer and parent, to get back to it when we're done examining
examineInfo.previousLayer = item.gameObject.layer;
examineInfo.previousParent = item.transform.parent;
// change item layer to the examine pivot point layer
MiscOps.ChangeObjectLayer(item.gameObject, examinePivot.gameObject.layer);
// adding it as a child to our examine pivot
item.transform.parent = examinePivot.transform;
examineInfo.itemObj = item.gameObject;
// zeroing out its local pos and resettins the scale
examineInfo.itemObj.transform.localPosition = Vector3.zero;
// nuke gravity, if any
examineInfo.rigid = examineInfo.itemObj.rigidbody;
if (examineInfo.rigid != null)
examineInfo.rigid.useGravity = false;
// set strings
examineStrings = item.examineStrings;
currentStringIndex = 0;
UpdateText();
// finally show the examine window
Show = true;
examineArea.examineCamera.RefreshCamera();
}
public void Notify_DoneExaminingItem()
{
Show = false;
if (examineInfo.itemObj == null)
return;
// if it was active, activiate it
if (examineInfo.wasActive)
examineInfo.itemObj.SetActive(true);
// get its previous layer back
MiscOps.ChangeObjectLayer(examineInfo.itemObj, examineInfo.previousLayer);
// if it has a rigid body, get its gravity back
if (examineInfo.rigid != null)
examineInfo.rigid.useGravity = true;
// free it, get its parent back
examineInfo.itemObj.transform.parent = examineInfo.previousParent;
// done
examineInfo.itemObj = null;
doneExamining = true;
}
public bool Show
{
get { return show; }
set
{
show = value;
examineGuiRoot.SetActive(show);
}
}
public class ItemToExamineInfo
{
public GameObject itemObj;
public Rigidbody rigid;
public bool wasActive;
public int previousLayer;
public Transform previousParent;
}
Thanks all. I hope this is the last problem in this series.
This is brilliant! It worked like charm! - Please make it an answer so that I could accept it and close the Q. However, I would like you to elaborate a bit more as to why it did work. Namely, I didn't get what you meant with margin. When I didn't add anything (just size = bounds.extents.y), the gun was 'slightly' bigger than the viewport, however, if I update (re-calculate) the cam's vp again, it would fit! I don't know why. And also why extents.y, I mean, I know that extents is half a bound, but why the y coords? Thanks a bunch.
btw extents.x also works, but makes the gun a bit smaller than what I want (increases the size more than I need).
I just worked that solution myself - for the purpose of taking orto screenshots. And an important issue was also to position - center the camera properly - and far enough to prevent occlusion. "y" just means the vertical dimension.
I tried 'wider' weapons, It is not 100% perfect, but works well for me, I'd have to tweak it a bit.
Well in my situation with screenhots - as the next step I was setting an arbitray renderTexture height, and then I was adjusting the RT width proportionally, basing on the rendererBounds.extents.x . You could just find some analogy.
Answer by tomekkie2 · Aug 22, 2013 at 08:45 AM
I think you have to determine camera view height with:
currentCamera.orthographicSize = rendererBounds.extents.y + margin;
if you look at it from front. And then determine the viewport proportions according to the object proportions. At least that has worked for me.
Your answer
Follow this Question
Related Questions
Ortho camera viewport setup? 3 Answers
Prevent objects from disappearing when zooming in on an orthographic scene? 1 Answer
Center Object in Viewport 1 Answer
Help re-calculating camera's viewport on UI drag (when the required area changes)? 1 Answer
Why Is My Orthographic Camera Not Rendering at the Full Resolution? 1 Answer