A 2D camera in Unity lets you control what part of your game world the player sees. It helps focus on the important action and creates a smooth experience.
2D camera setup in Unity
Camera.main.orthographic = true;
Camera.main.orthographicSize = 5f;
Camera.main.transform.position = new Vector3(x, y, z);Use orthographic = true to make the camera 2D (no perspective).
orthographicSize controls how much of the scene is visible vertically.
Camera.main.orthographic = true;
Camera.main.orthographicSize = 5f;Camera.main.transform.position = new Vector3(player.transform.position.x, player.transform.position.y, -10f);Camera.main.orthographicSize = 3f; // Zoom in Camera.main.orthographicSize = 10f; // Zoom out
This script sets the camera to 2D mode and smoothly follows the player with a fixed offset behind on the z-axis.
using UnityEngine; public class CameraFollow2D : MonoBehaviour { public Transform player; public float smoothSpeed = 0.125f; public Vector3 offset = new Vector3(0, 0, -10); void Start() { Camera.main.orthographic = true; Camera.main.orthographicSize = 5f; } void LateUpdate() { Vector3 desiredPosition = player.position + offset; Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); transform.position = smoothedPosition; } }
Always keep the camera's z-position negative in 2D so it can see the scene.
Use LateUpdate() to move the camera after the player moves for smooth following.
Adjust orthographicSize to control zoom level; smaller means zoomed in.
Set the camera to orthographic mode for 2D games.
Use the camera's position to control what part of the scene is visible.
Follow the player smoothly by updating the camera position each frame.
Practice
Orthographic mode in a 2D game?Solution
Step 1: Understand camera modes in Unity
Unity cameras can be set to Perspective or Orthographic. Perspective shows depth with size changes, Orthographic does not.Step 2: Identify the effect of Orthographic mode
Orthographic mode removes perspective distortion, making objects appear the same size regardless of distance, ideal for 2D games.Final Answer:
It removes perspective, showing objects without depth distortion. -> Option BQuick Check:
Orthographic = No perspective distortion [OK]
- Confusing orthographic with perspective mode
- Thinking orthographic adds 3D effects
- Assuming orthographic auto-follows player
Solution
Step 1: Recall Unity Camera properties
The Camera class has a boolean property calledorthographicto switch modes.Step 2: Identify correct syntax
The correct syntax to enable orthographic mode iscamera.orthographic = true;. Other options use incorrect property names or methods.Final Answer:
camera.orthographic = true; -> Option DQuick Check:
Property name is orthographic [OK]
- Using incorrect property names like isOrthographic
- Trying to call a method instead of setting a property
- Assigning string or enum instead of boolean
Camera.main.orthographicSize = 5f; Debug.Log(Camera.main.orthographicSize);What will be printed in the console?
Solution
Step 1: Understand orthographicSize property
TheorthographicSizecontrols half the vertical size of the camera view in world units and can be set and read.Step 2: Analyze the code output
The code setsorthographicSizeto 5f, then logs it. So the output will be 5.Final Answer:
5 -> Option AQuick Check:
Set orthographicSize = 5, output = 5 [OK]
- Thinking orthographicSize doubles the value
- Assuming orthographicSize is read-only
- Confusing orthographicSize with field of view
void Update() {
Vector3 targetPos = player.transform.position;
targetPos.z = -10;
transform.position = targetPos;
}
The camera jumps abruptly instead of moving smoothly.Solution
Step 1: Identify the problem with abrupt movement
Directly setting transform.position to player position causes instant jumps without smooth transition.Step 2: Apply smooth movement using interpolation
UsingVector3.Lerpblends current and target positions over time, creating smooth camera movement.Final Answer:
Use Vector3.Lerp to interpolate between current and target positions. -> Option AQuick Check:
Smooth follow = Vector3.Lerp [OK]
- Ignoring z-axis setting causing camera clipping
- Using transform.Translate incorrectly
- Removing z-axis assignment breaks camera depth
Solution
Step 1: Calculate orthographicSize for 20 units vertical view
Orthographic size is half the vertical size, so 20 units vertical means orthographicSize = 10.Step 2: Set camera position with offset and smooth follow
Adding offset (2,3) to player position and setting z to -10 keeps camera behind. Using Vector3.Lerp smooths movement.Final Answer:
camera.orthographicSize = 10f; Vector3 target = player.position + new Vector3(2, 3, -10); transform.position = Vector3.Lerp(transform.position, target, 0.1f); -> Option CQuick Check:
OrthographicSize = half vertical size, smooth follow with Lerp [OK]
- Setting orthographicSize equal to full vertical size
- Not using smooth interpolation for camera movement
- Using fieldOfView instead of orthographicSize for 2D camera
Ready to prove it?
Pass the test and this pattern moves unknown
