1
\$\begingroup\$

I am trying to get the bounds from an orthographic camera in Unity which take into account the current orthographic size (zoom).

I thought that I could use the cameraToWorldMatrix to transform the camera pixel rectangle into world coordinates.

var m = camera.cameraToWorldMatrix;

var bL = m.MultiplyPoint(new Vector3(camera.pixelRect.xMin, camera.pixelRect.yMin));
var uL = m.MultiplyPoint(new Vector3(camera.pixelRect.xMin, camera.pixelRect.yMax));
var uR = m.MultiplyPoint(new Vector3(camera.pixelRect.xMax, camera.pixelRect.yMax));

var rect = new Rect(uL.x, uL.y, Math.Abs(uR.x - uL.x), Math.Abs(bL.y - uL.y));

However, this does not take into account the orthographic size (zoom).

Multiplying the cameraToWorldMatrix with the camera's projectionMatrix property does not give me the correct results either.

Is there a sensible way that this can be solved?

\$\endgroup\$
3
\$\begingroup\$

Turns out that the answer is more simple than I originally thought.

        // Calculate orthographic camera bounds
        float height = 2f * camera.orthographicSize;
        float width = height * camera.aspect;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.