# Rotate perspective camera to align screen width to procedural object's width (Unity, C#)

I give up. After much trying and searching, I have to say I was unable to achieve the following task, for which I must call for your wise advice.

In my current Unity 5 application (using C#), there is a procedurally generated object (therefore with variable shape) to which the camera should be aligned when players hit a given key. By aligned, please understand it making the width of the screen parallel to the longest part of the object. The idea is represented in the following picture, where in black one can see the irregular object, in yellow is the bounding box.

In other words, it means that I want the camera to be rotated so to align to the longest side of the object's bounding box. I tried that. Still, I was unable to properly calculate the correct angles so to rotate the camera in a way that the horizontal axis of the screen becomes parallel to the width of the objects' bounding box.

Any help would be much appreciated.

Calculate the angle of the bounding box (or the angle between the 2 furthest points). Vector3.Angle(bounds.max - new Vector3(bounds.max.x, bounds.min.y, bounds.max.z), Vector3.right) Then simply set the camera's rotation to that angle. Repeat as necessary for each dimension.

Not sure if this would work for your game as you might’ve missed some details but if the position is always the same, why don’t you just hit the object with rays from all sides and determine with those values it’s dimensions.

You could position several “ray sources” from each horizontal, vertical and diagonal side and work with those values, so shortest distance is the longest side.

I can see how determining the shape would be difficult like that but if you run a few tests and develop an algorithm in a function that would do it for you.

Since it’s irregular this might be inaccurate though but it depends on how irregular it can be, in any case the solution would be: more rays