📖 Scenario: You are making a simple 2D game in Unity. You want to set up a camera that follows the player smoothly as they move around the scene.
🎯 Goal: Build a Unity script that makes the 2D camera follow the player object with smooth movement.
📋 What You'll Learn
Create a player GameObject variable
Create a smooth speed variable
Write code to move the camera smoothly towards the player position
Print the camera's position after moving
💡 Why This Matters
🌍 Real World
In many 2D games, the camera needs to follow the player smoothly to keep the gameplay visible and enjoyable.
💼 Career
Understanding camera control is important for game developers and Unity programmers to create polished game experiences.
Progress0 / 4 steps
1
Create a player variable
Create a public Transform variable called player to hold the player's position.
Unity
Hint
Use public Transform player; inside the class to hold the player object.
2
Add a smoothSpeed variable
Add a public float variable called smoothSpeed and set it to 0.125f.
Unity
Hint
Use public float smoothSpeed = 0.125f; to create the speed variable.
3
Write smooth follow logic in LateUpdate
Write a LateUpdate method that moves the camera smoothly towards the player position using Vector3.Lerp. Use smoothSpeed for interpolation. Keep the camera's original z position.
Unity
Hint
Use Vector3.Lerp to smoothly move the camera from its current position to the player's position.
4
Print the camera's position
Add a Debug.Log statement inside LateUpdate to print the camera's current position after moving.
Unity
Hint
Use Debug.Log("Camera position: " + transform.position); to print the camera's position.
Practice
(1/5)
1. What is the main reason to set a Unity camera to Orthographic mode in a 2D game?
easy
A. It increases the camera's field of view.
B. It removes perspective, showing objects without depth distortion.
C. It automatically follows the player character.
D. It enables 3D rendering with shadows.
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 B
Quick Check:
Orthographic = No perspective distortion [OK]
Hint: Orthographic means no depth, perfect for 2D views [OK]
Common Mistakes:
Confusing orthographic with perspective mode
Thinking orthographic adds 3D effects
Assuming orthographic auto-follows player
2. Which of the following is the correct way to set a Unity camera to orthographic mode in C#?
easy
A. camera.mode = Orthographic;
B. camera.isOrthographic = true;
C. camera.setOrthographic(true);
D. camera.orthographic = true;
Solution
Step 1: Recall Unity Camera properties
The Camera class has a boolean property called orthographic to switch modes.
Step 2: Identify correct syntax
The correct syntax to enable orthographic mode is camera.orthographic = true;. Other options use incorrect property names or methods.
Final Answer:
camera.orthographic = true; -> Option D
Quick Check:
Property name is orthographic [OK]
Hint: Use camera.orthographic = true to enable orthographic mode [OK]
Common Mistakes:
Using incorrect property names like isOrthographic
Trying to call a method instead of setting a property
The camera jumps abruptly instead of moving smoothly.
medium
A. Use Vector3.Lerp to interpolate between current and target positions.
B. Set targetPos.z = 0 instead of -10.
C. Call transform.Translate(targetPos) instead of setting position.
D. Remove the z-axis assignment to let camera auto-adjust.
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
Using Vector3.Lerp blends current and target positions over time, creating smooth camera movement.
Final Answer:
Use Vector3.Lerp to interpolate between current and target positions. -> Option A
Quick Check:
Smooth follow = Vector3.Lerp [OK]
Hint: Use Vector3.Lerp for smooth camera movement [OK]
Common Mistakes:
Ignoring z-axis setting causing camera clipping
Using transform.Translate incorrectly
Removing z-axis assignment breaks camera depth
5. You want to set up a 2D camera in Unity that shows exactly 20 units vertically and follows the player with an offset of (2, 3) units smoothly. Which code snippet correctly sets the orthographic size and follows the player with offset?
hard
A. camera.fieldOfView = 20f;
transform.position = player.position + new Vector3(2, 3, -10);
B. camera.orthographicSize = 20f;
Vector3 target = player.position + new Vector3(2, 3, 0);
transform.position = target;
C. camera.orthographicSize = 10f;
Vector3 target = player.position + new Vector3(2, 3, -10);
transform.position = Vector3.Lerp(transform.position, target, 0.1f);
D. camera.orthographicSize = 5f;
transform.position = player.position + new Vector3(2, 3, -10);
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 C
Quick Check:
OrthographicSize = half vertical size, smooth follow with Lerp [OK]
Hint: OrthographicSize = half vertical view; use Lerp for smooth follow [OK]
Common Mistakes:
Setting orthographicSize equal to full vertical size
Not using smooth interpolation for camera movement
Using fieldOfView instead of orthographicSize for 2D camera
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.