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 /
avatar image
0
Question by Syl_Creates · Mar 14, 2020 at 11:30 AM · camerapositionobjectviewmaze

Object to appear in FULL in camera view no matter the size

Hi everyone. I'm very very new to Unity so please go easy on me but basically I have a problem that I do not know how to solve for the life of me. So I have to create a maze Generator(which I have done) but how can I make sure that the entire maze will be within camera view and with minimum white space? Any answers or code snippets would be very appreciated. Thank you so much.

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
2
Best Answer

Answer by metalted · Mar 15, 2020 at 03:42 PM

This script will work with orthographic cameras. You can make the script work with perspective cameras but it will take a bit of tinkering. Added comments for further explanation. To work right, you need to make sure that the center of your maze and the camera are aligned.

 using UnityEngine;
 
 public class FillScreen : MonoBehaviour
 {
     public float perspectiveCompensation = 0.95f;
 
     void Start()
     {
         Camera cam = Camera.main;
         Bounds bounds = GetComponent<MeshRenderer>().bounds;
         Vector2 screenSize = new Vector2(Screen.width, Screen.height);
 
         //Get the position on screen.
         Vector2 screenPosition = cam.WorldToScreenPoint(bounds.center);
         //Get the position on screen from the position + the bounds of the object.
         Vector2 sizePosition = cam.WorldToScreenPoint(bounds.center + bounds.size);
         //By subtracting the screen position from the size position, we get the size of the object on screen.
         Vector2 objectSize = sizePosition - screenPosition;
         //Calculate how many times the object can be scaled up.
         Vector2 scaleFactor = screenSize / objectSize;
         //The maximum scale is the one form the longest side, with the lowest scale factor.
         float maximumScale = Mathf.Min(scaleFactor.x, scaleFactor.y);
 
         if (cam.orthographic)
         {
             //Scale the orthographic size.
             cam.orthographicSize = cam.orthographicSize / maximumScale;
         }
         else
         {
             //Set the scale of the object.
             transform.localScale = transform.localScale * maximumScale * perspectiveCompensation;
         }
     }
 }




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 Syl_Creates · Mar 19, 2020 at 06:30 PM 0
Share

Thank you so much for the help, will try to use that in my code.

avatar image metalted Syl_Creates · Mar 19, 2020 at 06:39 PM 0
Share

No problem! If the answer works for you, please accept it as the answer so the question can be closed! Thank you!

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

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

Related Questions

View through objects 2 Answers

Check object is inside a "simulated" camera view 1 Answer

Make object appear within camera view 0 Answers

How to apply a force forward depending of the camera 2 Answers

Rotating Camera around Player at Certain Point in Map 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