• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by vexe · Aug 22, 2013 at 08:25 AM · cameracoordinatesnguiviewportorthographic

How to resize a camera's orthoSize for an object to fit inside its rect viewport?

Hello, t$$anonymous$$s is another follow-up of t$$anonymous$$s series of questions. So I'm still working on the Examine feature of Resident Evil.

Short summary of my setup once again:

  1. I'm using NGUI, I have a "Examine_UI" UI, w$$anonymous$$ch has its main camera (w$$anonymous$$ch you can see in the left image below). T$$anonymous$$s camera can only see the UI layer, w$$anonymous$$ch is "2D_Examine"

  2. Going down you got "ExamineCamera" w$$anonymous$$ch only sees the "ExamineArea" (grey window in the middle of the UI, left image) and its c$$anonymous$$ldren.

alt text

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, w$$anonymous$$ch 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.

alt text

I can t$$anonymous$$nk of 2 ways of ac$$anonymous$$eving t$$anonymous$$s:

  1. If the gun was bigger/smaller, I would keep increasing/decreasing the size of my orthograp$$anonymous$$c 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?

  2. Instead of changing the camera's size, I could maybe scale down the gun till it fits. Here, I t$$anonymous$$nk 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 ac$$anonymous$$eve it? change the cam's orthoSize till the gun fits the window? Is t$$anonymous$$s possible or am I gonna have to go with the 2nd method, w$$anonymous$$ch I'm also having trouble with :/ - I just want ideas, thoughts, not code.

Please notice that I only want to do t$$anonymous$$s t$$anonymous$$ng 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 anyt$$anonymous$$ng
     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 c$$anonymous$$ld 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 t$$anonymous$$s is the last problem in t$$anonymous$$s series.

re5.png (186.2 kB)
difsizes.png (112.8 kB)
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Aug 22, 2013 at 09:28 AM 1
Share
avatar image vexe · Aug 22, 2013 at 09:34 AM 1
Share
avatar image tomekkie2 · Aug 22, 2013 at 09:41 AM 1
Share
avatar image vexe · Aug 22, 2013 at 09:50 AM 1
Share
avatar image tomekkie2 · Aug 22, 2013 at 10:04 AM 0
Share
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by tomekkie2 · Aug 22, 2013 at 08:45 AM

I t$$anonymous$$nk you have to determine camera view height with:

 currentCamera.orthograp$$anonymous$$cSize = 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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shacharoz · Jan 09, 2022 at 05:34 PM 0
Share

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

is it possible to set the view port rect at both ends 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges