GalluskY
GalluskY

Reputation: 53

Unity : Keep gameobject in camera view no matter it's size

I'm developing a little game where I generate rooms of different size and would like this randomly generated room to be always visible on the screen without caring about it's size. The camera is on a top view angle (rotation = 90,0,0).

I tried to create a relationship between it's size and the Y axis position of the camera to make it always visible but it wasn't successful. There is the solution where the object is kept on the bottom left corner of the screen but if the object is too big only a part of it is visible by the camera. I really have no more idea ^^

Thank you for your help !

Upvotes: 1

Views: 2813

Answers (1)

Rifat Bin Reza
Rifat Bin Reza

Reputation: 2761

I guess you are using an Orthographic camera. For the Orthographic Camera, the size is the number of unity units from the center of the camera to the top/bottom of the screen. The width is then based on the aspect ratio. So, if you know how big the objects are that should be easy!

You can get or set the main camera size with Camera.main.orthographicSize

then get/set the aspect ratio (width/height) with Camera.main.aspect

and you can reset it after with Camera.main.ResetAspect();

Upvotes: 2

Related Questions