|

Finding World Height and Width from Camera in Unity

I saw this question pop up a few times on the forums recently so thought I’d share a quick tip on how to easily do this. The question is “How do I find the height and width in WORLD units that is visible in the current camera view”.

It’s actually quite easy, here’s a quick script that uses Camera.ScreenToWorldPoint() to get the width. Assuming your camera is at (0,0) then by multiplying the result by 2, you get the actual width in game/world units.

    public float Width
    {
        get
        {
            Vector3 screenWidth = new Vector3(Screen.width, 0);
            return _camera.ScreenToWorldPoint(screenWidth).x * 2;
        }
    }

    public float Height
    {
        get
        {
            Vector3 screenHeight = new Vector3(Screen.height, 0);
            return _camera.ScreenToWorldPoint(screenHeight).y * 2;
        }
    }

Short post, but it does what it’s supposed to. Depending on whether your view changes, you may wish to cache the result and calculate the sizes else where.

Interested in more Unity related tricks and tutorials? Check them out here.

Jonathan

Jonathan

Hey there, I’m Jon! I love landscape photography which is why I travel all the time! On my blog I’ll share all the best spots for epic sunsets and sunrises as well as some photography tips and tricks. I also have a interest in game design so you’ll find the occasional post about that too. Thanks for visiting!

Similar Posts

guest

0 Comments
Inline Feedbacks
View all comments