- Home /
using objects bounds to zoom in object
hi everybody! i am trying to figure out how to calculate the zoom of the object based on its bounds so it fills out the screen completly.i have a lot of objects in the scene and all of them are different sizes, when i click on some of them, the zoom in, the smaller ones dont get zoomed in enough. i have a constant zoom factor which i would like to calculate based on objects bounds. how should i do this? does screen width and height plays any role here?
Just glanced at this thread, i have somethine that is abit inversly true:
its a code that makes the object the same size regardless ofthe distance of teh camera by changing the object size as it moves away, it looks always the same...
ins$$anonymous$$d of obj size you can change the cam distance variable:
var worldSize :float = (2 * $$anonymous$$athf.Tan(fov / 2.0)) * testdistance;
var size :float = 0.25f * worldSize;
var screen = 50.0/$$anonymous$$athf.$$anonymous$$ax(Screen.width,Screen.height);
cube.transform.localScale = Vector3(screen*size, screen*size, 0.0001); //flat square seen from front, should
Answer by Tetrad · Jun 21, 2010 at 03:26 PM
Screen width/height shouldn't play a role. What should play a role is the camera's FOV (and possibly near/far clip planes). The FOV in Unity is vertical.
I don't know the math offhand, but you could figure out for a given height how far away from the camera you need to be for it. According to this thread, it's simply using the tangent of the FOV.
Alternatively, you might be able to whip up a solution by calculating the camera frustum planes and moving forward until they are no longer in bounds
although i didnt use your solution i thank you..i managed to resolve the issue in an uncomplicated way and that was to look for object bounds, like this collider.bounds.size, that is the Vector3, then to look the highest value and to set the distance to that value. in that way i have smaller and bigger object to fill out the screen when zoomed in... thanks everybody!
There is a caveat on that... If you get the zoom ok for a PC with 1920 pixels, and you send your project to someone with a small screen or a smartphone, it could be unpredictable... but indeed it should be vaguely ok.
@pretender , could you tell me how to look at the highest value of vector3 ? i've same problem.
Answer by AnaRhisT · Jun 21, 2010 at 10:46 AM
You can give them all tags, like size1, size2, size3, raycast and check the tags, if it's size1 so it'll zoom 2, size2 3.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062613im_/https://answers.unity.com/themes/thub/images/avi.jpg)