Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ibrahimAlfors · Mar 03, 2016 at 12:24 PM · camerascaleplanefield of view

Dynamically adjust any plane infront of Camera

Hello everyone,

I'm working on an algorithm that would place a game object infront of the Camera, the game object would be adjusted in a way that it fits in the camera point of view regardless of its size.

The way I tried to do it is by increasing the distance between the game object and the camera if the game object is large and decreasing it if the game object is small. My code works well with all types of game objects with various scales however, when it comes to planes, it just doesn't.

I did my research and found out that the planes are supposed to be scaled x10 of a cube for example. I tried to include this factor in my code but the results were inconsistent and dependable on the size of the plane. If the plane is too large it would not fit into the field of view and if the scale is too small it would fit but wouldn't fill the field of view.

This is the cod I wrote:

 public void moveObject(){
         float screenWidth = Screen.width;
         float screenHeight = Screen.height;
 
         float objectWidth = gameObject.transform.localScale.x;
         float objectHeight = gameObject.transform.localScale.y;
         var frustumWidth = 0f;
         var frustumHeight = 0f;
         var distance = 0f;
 
         if (objectHeight / objectWidth > screenHeight / screenWidth) {
 
             frustumWidth = ((gameObject.transform.localScale.y*10)+30) * mainCamera.camera.aspect;
             //the *10 in the above is where I factored the scale of the plane
 
             frustumHeight = frustumWidth / mainCamera.camera.aspect;
             Debug.Log("do height");
         } else {
             Debug.Log("do width");
             frustumHeight = ((gameObject.transform.localScale.x*10)+20) / mainCamera.camera.aspect;
             //the *10 in the above is where I factored the scale of the plane
         }
         distance = frustumHeight * 0.5f / Mathf.Tan(mainCamera.camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
         Debug.Log ("Distance is " + distance);
         gameObject.transform.localRotation = mainCamera.transform.localRotation;
         gameObject.transform.SetParent (mainCamera.transform);
         
         gameObject.transform.localPosition = new Vector3(0 , 0, distance);
         
     }

The above code isn't giving me the right distance so the plane would fit regardless its scale. The code works well (without the factoring) for cubes and other types of gameobjects so I feel like its a small thing I'm missing, I would appreciate it if someone could take a look at that.

Thanks everyone.

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 ibrahimAlfors · Mar 01, 2016 at 01:44 PM 0
Share

And I immediately found the answer lol

what made the results inconsistant was the (30) and the(20) that I added as padding, logically if the distance between the camera and the plane is large, the padding will seem smaller because it is a constant value, and if the the distance is small the padding will seem larger as well.

So I left the question and as soon as it is submitted I will add the answer for anyone who would like to use the code. its fairly simple.

Thanks.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Confine mouse to a plane 1 Answer

I can't fit a plane to my field of view 2 Answers

camera force scaling child objects SOLVED 1 Answer

Changing Camera Field of View 0 Answers

changing the scale of camera in VR 0 Answers


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