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 slgooding · Jun 25, 2012 at 09:48 PM · camerafov

Keep GameObject in Camera View as it Grows Vertically

I have a group of gameobjects that "grow" vertically (think of a plant growing). The camera view starts at the ground level, where the plant sprouts from the dirt. I would like to have the camera smoothly zoom out as the plant grow to keep the entire plant in view. How would I go about doing this?

I know that I can use the minimum/maximum Y values, but how do I use that to position the camera so that the objects are within the camera's field of view?

Thanks!

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
1

Answer by hathol · Jun 25, 2012 at 11:27 PM

probably not the most efficient way since my maths skills are still a bit rusty, but it will work (or at least give you a starting point) :)

 public GameObject m_target;

 void Update () 
 {
     float gamma = camera.fieldOfView/2f;
     float alpha = 90f-gamma;
     float c = Mathf.Abs(m_target.transform.localScale.y/2f);
     
     float sinA = Mathf.Sin(alpha*Mathf.Deg2Rad);
     float sinC = Mathf.Sin(gamma*Mathf.Deg2Rad);
     // a/sin(alpha) = c/sin(gamma)
     // a = (c*sin(alpha))/sin(gamma)
     
     float a = (c*sinA)/sinC;
     Vector3 newPos = m_target.transform.position; // assuming the object pivot is at its center
     newPos.z -= a+3; // give it a bit of extra space
     transform.position = newPos;
 }
Comment
Add comment · Show 2 · 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 slgooding · Jun 26, 2012 at 12:14 AM 0
Share

$$anonymous$$, this works great! THAN$$anonymous$$ YOU. I couldn't have figured this out without your help. Now I just need to add some lerping to smooth out the new position change.

Now I just wish I had paid more attention during my math courses :). Just out of curiosity, if I wanted to learn the math behind this, what should I google?

Thanks again :).

avatar image hathol · Jun 26, 2012 at 12:23 AM 1
Share

Don't worry, you'll learn most of it as you go along :). For the maths behind: just search for "solving a triangle". We have a given side (c, your y extents) and 2 given angles (FOV/2 is the angle for c and since the camera is looking straight at the object, the angle between that axis and c must be 90 degrees). Then you can just use the law of sines to solve it ( I googled that one ;) )

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera movement in a 2D? (like angry birds) 1 Answer

Know if gameObject is being seen 2 Answers

Orthographic camera "cutting off" part of the frame 2 Answers

Minimap camera display size. 1 Answer

How to accurately zoom (not dolly) and pan Camera to mouse click position 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