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 axCed · Aug 13, 2014 at 10:24 PM · camerascalecubezoomperspective

How to do perspective camera zoom?

We have a perspective camera looking at a cube that scales (increasing and decreasing) almost every frame.

The goal is that no matter the scaling of the rectangle we will always see it near the borders as the camera will dynamically adjust itself. When its scale/size is inside of the screen the camera should zoom in and when its scale/size is outside of the screen the camera should zoom out.

To adjust the camera on the cube, we tried to set the field of view (both vertically and horizontally) and we also tried to translate the camera by calculating the distance between the camera and the cube. In both cases we find it difficult to do and we have a little issue where the camera is jumping temporary between bool IsInSceen() when the cube is not scaling. So we would like a little help.

Comment
Add comment · Show 1
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 Tanshaydar · Aug 13, 2014 at 10:47 PM 0
Share

Depending on the size change you might wanna change the fov value of the camera with a specific value like 1.6 times more or less?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Aug 13, 2014 at 11:14 PM

You will find the calculations for a perspective camera here:

http://docs.unity3d.com/Manual/FrustumSizeAtDistance.html

With a bit of algebra, you can solve for whatever you need to...adjust the FOV or move the camera. There are some open questions about exactly what you need from your description. So I'm going to give you a bit of simple starter code for a restricted situation. Let's assume:

  • The block has some 'z' depth

  • The block is always taller than the aspect ratio of the camera (i.e. it is the height of the cube that determines the camera zoom.

  • Zoom is done though movement, not changing the FOV.

  • The camera is at (0,0,-something) with a rotation of (0,0,0).

  • The cube is centered in the camera

Here is a bit of code. Start a new scene, attach the script below. Run the app and modify the size of the block in the Inspector.

 #pragma strict
 
 function Update() {
     var frustumHeight = transform.localScale.y;
     var distance = frustumHeight * 0.5 / Mathf.Tan(Camera.main.fieldOfView * 0.5 * Mathf.Deg2Rad);
     
     // Since front side of the block is not at pivot
     distance += transform.localScale.z * 0.5;

     Camera.main.transform.position = Vector3.back * distance;
 
 }

Comment
Add comment · Show 3 · 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 Bunny83 · Aug 13, 2014 at 11:32 PM 2
Share

Using camera movement is almost never used to implement a zoom function. That's simply because you would easily glitch through near objects. Almost all games i know (mainly FPS games) change the FOV to zoom in or out. A movement approach might work for games with a fix camera like RTS games with a top-down / iso camera, but for anything more dynamic you usually want to use the FOV ins$$anonymous$$d.

avatar image axCed · Aug 14, 2014 at 02:16 AM 0
Share

Thank you robertbu. I'll go with the fov coding. That's a pretty cool link you shared with us. Thank you. We'll start to work with it and we will give you some feedback when it's done.

Thank you Bunny83 for the comment. You are right. And besides, the clipping planes issue is not only for near objects but also for the ones far away especially with an angle to the camera.

avatar image robertbu · Aug 14, 2014 at 02:18 AM 0
Share

@Bunny83 - good to know:

@axCed - a bit of example code with the same restrictions as the previous that zooms using field of view:

 #pragma strict
 
 function Update() {
     var frustumHeight = transform.localScale.y;
     var distance = $$anonymous$$athf.Abs(transform.position.z - Camera.main.transform.position.z) - transform.localScale.z * 0.5;
      Camera.main.fieldOfView = 2.0 * $$anonymous$$athf.Atan(frustumHeight * 0.5 / distance) * $$anonymous$$athf.Rad2Deg;
 }

Your criteria is fuzzy, so I expect you'll have to do more, but this will get you started.

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

22 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 avatar image avatar image avatar image

Related Questions

Is there a way to zoom GUI and scene at the same time? 0 Answers

Stop main camera from zooming 2D 1 Answer

Accurate Pinch Zoom 1 Answer

Need help with 2D orthographic camera zooming 2 Answers

Keep line renderer width same on screen no matter camera distance 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