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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera Field of View 2 Answers

How to create a simple mobile water inside a 3D container? 0 Answers

Smooth billboard rotation and srite jitter? 1 Answer

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

Draw on a Plane surface with varying camera positions 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges