In Unity, the Camera is just another GameObject with a Camera component attached to it.
- Every new 3D scene comes with a Main Camera by default
- You can move, rotate, and scale it like any GameObject
- The Camera component determines what the player sees on screen
Orthographic vs Perspective Camera
The Camera component has two projection modes that change how objects are rendered.
- Perspective (3D): Objects appear smaller as they move away (realistic). Has Field of View property (typically 60-90 degrees). Used for FPS, RPG, and most 3D games.
- Orthographic (2D): Objects stay the same size regardless of distance (flat). Has Size property instead of FOV. Used for 2D platformers, strategy games, and minimaps.
Simple Camera Follow
This is the most basic camera control – the camera strictly follows the target position.
public class SimpleFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset = new Vector3(0, 2, -10);
void LateUpdate()
{
transform.position = target.position + offset;
}
}
- offset creates distance between camera and target
- Use
LateUpdate()instead ofUpdate()– camera moves after player, preventing jitter
Smooth Camera Follow
A smooth follow creates a polished, professional feel instead of instant snapping.
public class SmoothFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public float smoothSpeed = 5f;
void LateUpdate()
{
Vector3 desired = target.position + offset;
transform.position = Vector3.Lerp(transform.position, desired, smoothSpeed * Time.deltaTime);
}
}
- Vector3.Lerp() linearly interpolates between current and desired position
- Higher smoothSpeed means faster following
Third-Person Orbit Camera
This camera rotates around the player when the mouse moves – common in action RPGs.
public class OrbitCamera : MonoBehaviour
{
public Transform target;
public float distance = 5f;
public float sensitivity = 2f;
private float yaw = 0f;
private float pitch = 0f;
void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * sensitivity;
pitch -= Input.GetAxis("Mouse Y") * sensitivity;
pitch = Mathf.Clamp(pitch, -30f, 60f);
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
transform.position = rotation * new Vector3(0, 0, -distance) + target.position;
transform.LookAt(target);
}
}
- Yaw: Horizontal rotation (left/right)
- Pitch: Vertical rotation (up/down)
- Mathf.Clamp() prevents looking too far up or down
First-Person Mouse Look
For FPS games, the camera acts as the player's eyes and is attached to the player body.
public class FirstPersonLook : MonoBehaviour
{
public float sensitivity = 2f;
private float pitch = 0f;
void Update()
{
float yaw = Input.GetAxis("Mouse X") * sensitivity;
transform.parent.Rotate(0, yaw, 0); // Rotates the body left/right
pitch -= Input.GetAxis("Mouse Y") * sensitivity;
pitch = Mathf.Clamp(pitch, -90f, 90f);
transform.localRotation = Quaternion.Euler(pitch, 0, 0); // Rotates only camera up/down
}
}
- Camera must be a child of the player body GameObject
- Body rotates left/right (yaw), camera rotates up/down (pitch)
- Prevents the entire body from tilting when looking up
Camera Shake
Camera shake adds impact feedback for explosions, hits, or landings.
public class CameraShake : MonoBehaviour
{
private Vector3 originalPos;
private float shakeDuration = 0f;
void Start() => originalPos = transform.localPosition;
void Update()
{
if (shakeDuration > 0)
{
transform.localPosition = originalPos + Random.insideUnitSphere * 0.1f;
shakeDuration -= Time.deltaTime;
}
else
transform.localPosition = originalPos;
}
public void Shake(float duration) => shakeDuration = duration;
}
- Random.insideUnitSphere creates random offset in all directions
- Call Shake(0.3f) when player takes damage or an explosion occurs
Camera Zoom
Zoom changes the camera's field of view (3D) or orthographic size (2D) for effects like aiming or scoping.
For 3D (Perspective camera)
float scroll = Input.GetAxis("Mouse ScrollWheel");
cam.fieldOfView -= scroll * zoomSpeed;
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, 30f, 90f);
For 2D (Orthographic camera)
cam.orthographicSize -= scroll * zoomSpeed;
cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, 2f, 10f);
Common Mistakes
- Using Update() instead of LateUpdate() – causes camera lag behind player
- Not clamping pitch values – camera flips over when looking straight up/down
- Moving camera with physics in FixedUpdate() – use LateUpdate() instead
- Forgetting to test different aspect ratios – camera may show unintended areas