CameraController
A straightforward camera controller that follows the player’s position in 2D space.Configuration
Reference to the player’s PlayerInput component for position tracking
Implementation
CameraController.cs:3-22
How It Works
Initialization
On startup, the camera initializes its target position:CameraController.cs:10-12
-10 to position the camera in front of 2D sprites (which are typically at Z=0).
In Unity’s 2D mode, negative Z values place the camera in front of sprites. The value
-10 is a common convention that ensures proper rendering without being too far away.Position Tracking
Every frame, the camera updates to match the player’s position:CameraController.cs:14-20
- Reads the player’s X and Y coordinates
- Maintains its Z-coordinate at
-10 - Instantly moves to the target position
This implementation provides instantaneous following without smoothing. The camera position updates every frame in
Update() rather than FixedUpdate() for smooth visual tracking independent of physics updates.Setup in Unity
-
Main Camera Setup
- Select the Main Camera in the Hierarchy
- Add the CameraController component
- Set the camera’s Projection to Orthographic (for 2D)
- Adjust Size to set the zoom level (e.g., 5-10)
-
Player Reference
- Drag the Player GameObject into the
playerfield - The player must have a PlayerInput component
- Drag the Player GameObject into the
-
Camera Positioning
- Initial camera position doesn’t matter - it will snap to the player in
Start() - Ensure Z position is negative (handled automatically)
- Initial camera position doesn’t matter - it will snap to the player in
Customization Options
While the base implementation is simple, here are common enhancements:Smooth Following (Lerp)
Add interpolation for smoother camera movement:Camera Bounds
Prevent the camera from moving outside level boundaries:Dead Zone
Only move the camera when the player moves beyond a certain distance:Look-Ahead
Offset the camera slightly in the player’s movement direction:Y-Axis Only
For side-scrollers where horizontal position is automatic:Integration with Player System
The camera system integrates simply with the player:The camera uses
PlayerInput as its reference, but only accesses the Transform component. It doesn’t call any PlayerInput methods or modify player state.Performance Considerations
Update vs LateUpdate
For smoother following, consider usingLateUpdate():
LateUpdate() runs after all Update() calls, ensuring the camera moves after the player has finished moving for the frame.
Vector3 Allocation
The current implementation modifies the sametarget vector, which is efficient:
Common Issues
Camera Jitter
Problem: Camera appears to jitter or shake Solution: UseLateUpdate() instead of Update() so the camera moves after player physics
Player Off-Center
Problem: Player doesn’t appear centered in the camera view Solution: Verify the camera’s Z position is negative (e.g., -10) and the player’s Z position is 0No Camera Movement
Problem: Camera stays at starting position Solution: Check that:- The
playerfield is assigned in the Inspector - The player GameObject is actually moving
- The CameraController script is enabled
Best Practices
-
Use LateUpdate - Move the camera in
LateUpdate()for smoother following -
Maintain Z-coordinate - Always keep Z at
-10to ensure proper 2D rendering - Reference the player correctly - Ensure the player reference is assigned before Play mode
- Consider smoothing - Instant following can feel jerky; consider adding smooth damping
- Set appropriate camera size - Adjust orthographic size based on your game’s scale
- Test camera bounds - Ensure the camera doesn’t show areas outside your level
- Profile performance - Camera updates run every frame; keep the logic simple
Comparison with Cinemachine
Unity’s Cinemachine package provides advanced camera features:| Feature | CameraController | Cinemachine |
|---|---|---|
| Simple following | ✓ | ✓ |
| Smoothing | Manual | Built-in |
| Dead zones | Manual | Built-in |
| Camera shake | Manual | Built-in |
| Multiple targets | Manual | Built-in |
| Complexity | Low | High |
| Performance | Optimal | Good |
| Setup time | Minutes | Hours |
The simple CameraController is perfect for prototypes and small games. For production games with complex camera requirements, consider upgrading to Cinemachine.