Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Align camera to plane/quad boundaries

Discussion in 'Scripting' started by derpiet, Jan 9, 2013.

  1. derpiet

    derpiet

    Joined:
    Dec 21, 2012
    Posts:
    1
    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?
     
  2. KevinCodes4Food

    KevinCodes4Food

    Joined:
    Dec 6, 2013
    Posts:
    61
    Years without an answer? :)

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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScreenScaledQuad : MonoBehaviour {
    5.  
    6.     public ScreenScaleMode scaleMode = ScreenScaleMode.ScaleToWidthAndHeight;
    7.  
    8.     public enum ScreenScaleMode
    9.     {
    10.         ScaleToWidthAndHeight,
    11.         ScaleToWidthOnly,
    12.         ScaleToHeightOnly
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         float quadDepth = Camera.main.transform.position.z - transform.position.z;
    19.  
    20.         Vector3 tr = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, quadDepth));
    21.         Vector3 tl = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, quadDepth));
    22.  
    23.         // DEFAULT: Scale To Width & Height
    24.         float aspectRatio = transform.localScale.y / transform.localScale.x;
    25.         float widthScale = (tl.x-tr.x);
    26.         float heightScale = widthScale;
    27.  
    28.         // WIDTH ONLY?
    29.         if(scaleMode == ScreenScaleMode.ScaleToWidthOnly)
    30.         {
    31.             heightScale = widthScale * aspectRatio;
    32.         }
    33.  
    34.         // HEIGHT ONLY?
    35.         else if(scaleMode == ScreenScaleMode.ScaleToHeightOnly)
    36.         {
    37.             widthScale = heightScale / aspectRatio;
    38.         }
    39.  
    40.         transform.localScale = new Vector3(widthScale,heightScale,1);
    41.  
    42.  
    43.     }
    44. }
    45.  
     
    Last edited: Dec 28, 2014
    skullthug likes this.
  3. DarkOuts

    DarkOuts

    Joined:
    May 16, 2020
    Posts:
    1
    Still not working