Align camera to plane/quad boundaries

I’d like to align my orthographic MainCamera, so that a ground plane/quad in my scene always fills up the horizontal screen space.
At the moment I try to get the boundaries of the plane collider and move the camera in/out until the bounds are nearest to the edge of the screen.
Since it’s a mobile game, the aspect ratio and the screen size could be everything.

This seems to be a bit cumbersome, any Ideas how I could achieve the same effect with a more elegant solution?

Years without an answer? :slight_smile:

Throw this script on your quad and center it on the screen.

using UnityEngine;
using System.Collections;

public class ScreenScaledQuad : MonoBehaviour {

    public ScreenScaleMode scaleMode = ScreenScaleMode.ScaleToWidthAndHeight;

    public enum ScreenScaleMode
    {
        ScaleToWidthAndHeight,
        ScaleToWidthOnly,
        ScaleToHeightOnly
    }

    // Update is called once per frame
    void Update ()
    {
        float quadDepth = Camera.main.transform.position.z - transform.position.z;

        Vector3 tr = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, quadDepth));
        Vector3 tl = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, quadDepth));

        // DEFAULT: Scale To Width & Height
        float aspectRatio = transform.localScale.y / transform.localScale.x;
        float widthScale = (tl.x-tr.x);
        float heightScale = widthScale;

        // WIDTH ONLY?
        if(scaleMode == ScreenScaleMode.ScaleToWidthOnly)
        {
            heightScale = widthScale * aspectRatio;
        }

        // HEIGHT ONLY?
        else if(scaleMode == ScreenScaleMode.ScaleToHeightOnly)
        {
            widthScale = heightScale / aspectRatio;
        }

        transform.localScale = new Vector3(widthScale,heightScale,1);


    }
}
1 Like

Still not working