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 clculate the sizes else where.

Recommended Posts

Leave a Reply

avatar
  Subscribe  
Notify of