- Home /
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?
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);
Thank saschandroid, but i don't know why I can't get the mesh.bounds.size, it always return (0,0,0);
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);
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.
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.
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!
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.
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?