Tuesday, June 14, 2011

Unity: Fit to Zone Width/Height

So it turned out to be easy to calculate the required z-position of the camera to fit the view plane to the zone's width or height. Once I was thinking about it the right way. The code is below:

// Calculate the difference between viewplane and zone width.
float xFit = m_zone.Boundary.width / m_viewPlane.width;
float yFit = m_zone.Boundary.height / m_viewPlane.height;
// Apply to the existing z-pos of the camera.
if (m_zone.Fit == FitMode.FitHorizontal)
 z = position.z * xFit;
else if (m_zone.Fit == FitMode.FitVertical)
 z = position.z * yFit;

Stupidly easy.

And would you believe, I figured it out all out by myself?! The demo is up. Please forward all gold stars of achievement to my postal address.

No comments:

Post a Comment