Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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, 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:

  1. 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"

  2. Going down you got "ExamineCamera" which only sees the "ExamineArea" (grey window in the middle of the UI, left image) and its children.

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, 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.

alt text

I can think of 2 ways of achieving this:

  1. 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?

  2. 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.

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

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.

avatar image vexe · Aug 22, 2013 at 09:34 AM 1
Share

btw extents.x also works, but makes the gun a bit smaller than what I want (increases the size more than I need).

avatar image tomekkie2 · Aug 22, 2013 at 09:41 AM 1
Share

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.

avatar image vexe · Aug 22, 2013 at 09:50 AM 1
Share

I tried 'wider' weapons, It is not 100% perfect, but works well for me, I'd have to tweak it a bit.

avatar image tomekkie2 · Aug 22, 2013 at 10:04 AM 0
Share

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.

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 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.

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 at 05:34 PM 0
Share

can you please explain what renderBounds stands for?

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.

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

Changing Normalized Viewport Rect Coords at Runtime? 1 Answer

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


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