Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by xproject2013 · Nov 19, 2013 at 03:25 PM · camerascreenorthographicresize

Center an object in Ortographic view

Hi, I have a complicated problem to solve: I have a rectangular GameObject (a wall) that have to be centered inside an ortographic camera. It has to be zoomed in as much as possible but at the same time i need that he has a fixed padding from the sides of the screen (for example, from the wall at the edge of the screen there must always be a minimum of 60 px). The width of the wall is variable and therefore the size parameter must be recalculated each time after the wall resizing. I found this code to resize the wall height in order to fit the entire screen:

 Camera.main.orthographicSize = target.renderer.bounds.extents.y;

I tried to modify the code as follows but did not get a proper solution:

 public void changeOrthoSize () {
     GameObject wall = GameObject.FindGameObjectWithTag("Wall");
     float orthoSize;
     float rulerPadding = 60;
     if (wall.renderer.bounds.extents.x > wall.renderer.bounds.extents.y){
         // divide by 2 the x size to catch the y size
         if (wall.renderer.bounds.extents.x * 0.6f >= wall.renderer.bounds.extents.y){
             orthoSize = wall.renderer.bounds.extents.x * 0.6f + rulerPadding * 0.5f;
         } else {
             orthoSize = wall.renderer.bounds.extents.y + rulerPadding;
         }
     } else {
         orthoSize = wall.renderer.bounds.extents.y + rulerPadding;
     }
     Camera.main.orthographicSize = orthoSize;
 }


Anyone have any suggestions for this problem? Thanks in advance alt text

orthographic-view.jpg (48.0 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Nov 19, 2013 at 11:10 PM

You could try the following:

Cast a ray from the center of the object towards your camera (make it give you screen coordinates) and use this coordinate to center in on the screen.

Find the corners of the wall facing the camera, and cast a ray from them towards the camera. Change the camera's FoV depending on wether the four corner coordinates are on the screen (you could also add/substract your padding pixels to each X and Y value).

Comment
Add comment · Show 4 · 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 xproject2013 · Nov 20, 2013 at 09:26 AM 0
Share

Could you post a code example? In your explanation you talk about "FOV" but in orthographic camera i need to change the "size" parameters and it's not clear for me how to calculate it, thanks

avatar image Cherno · Nov 20, 2013 at 01:32 PM 0
Share

Whatever parameter controls the camera's field of view.

avatar image xproject2013 · Nov 20, 2013 at 06:14 PM 0
Share

Ok, if is possible can you post some code example? Thanks for your time

avatar image Cherno · Nov 20, 2013 at 08:29 PM 0
Share

I did some testing and got it to work. Note that you have to find out yourself how to get and update the correctVector3 of the WallCorner1 (should be the wall's upper left corner).

You could probably calculate the Vector3 of the corner based on the wall's starting x dimension and the current x scale.

I added the +5 and -5 to give some leeway when updating, otherwise the orthographicSize will jitter all the time because of constant Updating. You could probably make this smaller.

 var PlaneObject : GameObject;
 
 var HorizontalPadding : int;
 var VerticalPadding : int;
 
 var WallCorner1 : Vector3;
 
 var WallCorner1ScreenPoint : Vector3;
 
 function Update()
 {
     PlaneObject.transform.position = Camera.main.camera.transform.position + Camera.main.camera.transform.forward;
     
     WallCorner1ScreenPoint = Camera.main.camera.WorldToScreenPoint(WallCorner1);
     
     if(WallCorner1ScreenPoint.x < 0 + HorizontalPadding - 5)
     {
         Camera.main.camera.orthographicSize += 0.1;
     }
     if(WallCorner1ScreenPoint.x > 0 + HorizontalPadding + 5)
     {
         Camera.main.camera.orthographicSize -= 0.1;
     }
     
     if(WallCorner1ScreenPoint.y < 0 + VerticalPadding - 5)
     {
         Camera.main.camera.orthographicSize += 0.1;
     }
     if(WallCorner1ScreenPoint.y > 0 + VerticalPadding + 5)
     {
         Camera.main.camera.orthographicSize -= 0.1;
     }
 }

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

18 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

Related Questions

Orthographic camera cuts off half of screen in Web Player - resolution error? 1 Answer

Finding the right ortho size to fill the screen with an object 1 Answer

How to make WorldToScreenPoint work with an orthographic camera? 1 Answer

How to make WorldToScreenPoint work with an orthographic camera? 0 Answers

Orthographic cameras and screen resolutions 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