Skip to content

CameraScaler.cs

Err-orr edited this page May 16, 2025 · 4 revisions

Overview

The CameraScaler script is designed to automatically adjust the camera's position and orthographic size to fit the level's board within the camera's view. It ensures that the board is framed well, with some padding around it for a better visual experience. It centers the camera over the board and adjusts the zoom (orthographic size) based on whether the board is wider or taller. Padding and vertical offset help refine the framing, ensuring that the board is always well-presented in the camera's view. However, the board seems to not correctly resize when the board's height is greater than the width, which is an ongoing bug.

Requirements

This script must be attached to the Main Camera game object at ALL TIMES!!!

image

image

Logic

Variables

board

The board variable is used to reference to the Board object in the scene, and it's used to access the board's dimensions.

cameraOffset

This controls the camera's distance from the board along the Z-axis (depth), depending on the board's dimensions.

padding

This adds extra space around the board when calculating the camera's orthographic size to ensure the board fits comfortably in the frame. It makes sure that the board isn't awkwardly right next to the screen's resolution or if part of the board is being cut off.

aspectRatio

Defines the ratio for the camera's orthographic size, influencing how the camera scales the view based on the board’s dimensions.

yOffset

This is an upwards movement to the board. The value is small so it moves slightly up. This is to make the board appear more centered correctly.

Methods

Start()

This method is called once when the level is played. The camera is re-positioned to the board's size. image

repositionCamera()

The x and y values finds the middle of the board, the yOffset pushes the camera up slightly, and the cameraOffset pushes the camera away from the z-axis to give a "zoom out" effect. Then, the Main Camera game object moves its position based on the values. Then, if the board is wider than it is tall, then it adjusts for width. If it's taller than it is wider, then it adjusts for length. Padding is here so that the board doesn't look uncomfortable right up against the aspect ratio. image

Table of Contents

Introduction

Contribution

Scripts

Clone this wiki locally