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
2
Question by HonFai · May 22, 2016 at 08:22 AM · cameraimportinggame object

Auto scale camera to fit game object to screen center

I downloaded aiImporter for realtime import FBX object, but the object is too big and out of screen. What can I do? Is there any method to scale/translate camera to fit it?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by saschandroid · May 23, 2016 at 06:37 AM

You could move the camera dependent on the mesh bounds of your model (assuming your model is at (0, 0, 0)):

 Vector3 xyz = recap360GameObject.GetComponent<MeshFilter>().mesh.bounds.size;
 float distance = Mathf.Max(xyz.x, xyz.y, xyz.z);
 distance /= (2.0f * Mathf.Tan(0.5f * yourCamera.fieldOfView * Mathf.Deg2Rad));
 // Move camera in -z-direction; change '2.0f' to your needs
 yourCamera.transform.position = new Vector3(yourCamera.transform.position.x, yourCamera.transform.position.y, -distance * 2.0f);
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 HonFai · May 23, 2016 at 07:00 AM 0
Share

Thank saschandroid, but i don't know why I can't get the mesh.bounds.size, it always return (0,0,0);

avatar image HonFai · May 23, 2016 at 07:49 AM 0
Share

I modified the program and get it work, thanks a lot.

xyztemp= meshHolder.GetComponent().mesh.bounds.size; UnityEngine.Debug.Log ("xyz:" + xyz.magnitude);

                 if (xyz.magnitude<xyztemp.magnitude) {
                     xyz = xyztemp;
                     UnityEngine.Debug.Log (xyz.magnitude);
                     UnityEngine.Debug.Log (xyztemp.magnitude);
                 }
                 //UnityEngine.Debug.Log (xyz);
                 float distance = $$anonymous$$athf.$$anonymous$$ax(xyz.x, xyz.y, xyz.z);
                 distance /= (2.0f * $$anonymous$$athf.Tan(0.5f * Camera.main.fieldOfView * $$anonymous$$athf.Deg2Rad));
                 // $$anonymous$$ove camera in -z-direction; change '2.0f' to your needs
                 Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, -distance * 2.0f);
avatar image
0

Answer by alexhapki · May 22, 2016 at 10:25 AM

Can you just scale down the fbx object? When you import it, go to the object, on the Inspector of that object go to the Transform, reduce the "Scale" of the x,y and z numbers.

You could also do a prefab and reduce the scale of the prefab.

Comment
Add comment · Show 5 · 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 HonFai · May 23, 2016 at 02:07 AM 0
Share

It can't do that, it is because I use aiImporter for real time import fbx object. It must scale it by coding method, but i don't know how to do that.

avatar image alexhapki HonFai · May 23, 2016 at 04:38 AM 0
Share

Hi HonFai,

There are two quick ways I can think of to do what you need, I just made a quick video to explain you that, using the scenario I am working on for another project, so dont get confuse for many small blue bubbles! Video in YouTube: https://youtu.be/glsOmT7pzUA

  • 1) First way to do it; click and drag your fbx gameobject to a folder in your assets. That will créate a prefab. Change the transform scale on the Inspector to the size you need. Then use that prefab to instantiate at runtime.

  • 2) Second way; through scripting at runtime. Create a script and attach it to your gameobject. Add:

transform.localScale -= new Vector3(0.4f, 0.4f, 0.4); where 0.4f means you reduce the scale by 60% to a 40%. $$anonymous$$ore info on http://docs.unity3d.com/ScriptReference/Transform-localScale.html Note the InvokeReapeting doesnt have any effect, as it will always reduce to 40%, so you dont need an InvokeRepeat, just call once on Start() the method. I did it just out of curiosity.

I trust it helps you!

avatar image HonFai alexhapki · May 23, 2016 at 05:08 AM 0
Share

Hi alexhapki, The FBX is generate at runtime by using Recap 360, so I can't pre-import the object, my project is realtime generate all FBX object. And all object have their scale, so I don't know how to detect it.

Show more comments
avatar image
0

Answer by tlotw · Dec 17, 2020 at 10:21 PM

distance /= (2.0f Mathf.Tan(0.5f yourCamera.fieldOfView * Mathf.Deg2Rad));

I did not understand here. Would you please explain it in detail?

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object unaffected by image effects but can't be seen through walls? 1 Answer

game object appearing dark on one side 0 Answers

How to make camera position relative to a specific target. 1 Answer

Import Project from Asset store 6 Answers

rotate camera on mouse reaching edges 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