跳转至

Unity 6.3 — Cinemachine

最后验证: 2026-02-13 状态: 生产就绪 包: com.unity.cinemachine v3.0+(Package Manager)


概述

Cinemachine 是 Unity 的虚拟摄像机系统,无需手动编写脚本即可实现专业、动态的摄像机行为。它是 Unity 摄像机工作的行业标准。

将 Cinemachine 用于:

  • 第三人称跟随摄像机
  • 过场动画与电影序列
  • 摄像机混合与转场
  • 动态摄像机取景
  • 屏幕震动与摄像机特效

⚠️ Knowledge Gap: Cinemachine 3.0(Unity 6)是从 2.x 开始的一次重大重写。许多 API 名称和组件都发生了变化。


安装

通过 Package Manager 安装

  1. Window > Package Manager
  2. Unity Registry > 搜索 “Cinemachine”
  3. 安装 Cinemachine(版本 3.0+)

核心概念

1. 虚拟摄像机

  • 定义摄像机行为(位置、旋转、镜头)
  • 可存在多个虚拟摄像机;但同一时间只有一个处于“活动”状态

2. Cinemachine Brain

  • 位于主摄像机上的组件
  • 在虚拟摄像机之间进行混合
  • 将虚拟摄像机设置应用到 Unity Camera

3. 优先级

  • 虚拟摄像机具有优先级数值
  • 优先级最高的摄像机处于活动状态
  • 当优先级变化时平滑混合

基本设置

1. 将 Cinemachine Brain 添加到主摄像机

// Automatically added when creating first virtual camera
// Or manually: Add Component > Cinemachine Brain

2. 创建虚拟摄像机

GameObject > Cinemachine > Cinemachine Camera

这会创建一个具有默认设置的 CinemachineCamera GameObject。


虚拟摄像机组件

CinemachineCamera(Unity 6 / Cinemachine 3.0+)

using Unity.Cinemachine;

public class CameraController : MonoBehaviour {
    public CinemachineCamera virtualCamera;

    void Start() {
        // Set priority (higher = active)
        virtualCamera.Priority = 10;

        // Set follow target
        virtualCamera.Follow = playerTransform;

        // Set look-at target
        virtualCamera.LookAt = playerTransform;
    }
}

跟随模式(Body 组件)

第三人称跟随(轨道跟随)

// In Inspector:
// CinemachineCamera > Body > 3rd Person Follow

// Configure:
// - Shoulder Offset: (0.5, 0, 0) for over-shoulder
// - Camera Distance: 5.0
// - Vertical Damping: 0.5 (smooth up/down)

Framing Transposer(平滑跟随)

// CinemachineCamera > Body > Position Composer

// Configure:
// - Screen Position: Center (0.5, 0.5)
// - Dead Zone: Don't move camera if target within zone
// - Damping: Smooth following

硬锁定(精确跟随)

// CinemachineCamera > Body > Hard Lock to Target
// Camera exactly matches target position (no offset or damping)

瞄准模式(Aim 组件)

Composer(构图目标)

// CinemachineCamera > Aim > Composer

// Configure:
// - Tracked Object Offset: Aim at target's head instead of feet
// - Screen Position: Where target appears on screen
// - Dead Zone: Don't rotate if target within zone

注视目标

// CinemachineCamera > Aim > Rotate With Follow Target
// Camera rotation matches target rotation (e.g., first-person)

摄像机之间的混合

基于优先级的混合

public CinemachineCamera normalCamera; // Priority: 10
public CinemachineCamera aimCamera;    // Priority: 5

void StartAiming() {
    // Set aim camera to higher priority
    aimCamera.Priority = 15; // Now active
    // Brain automatically blends from normalCamera to aimCamera
}

void StopAiming() {
    aimCamera.Priority = 5; // Back to normal
}

自定义混合时间

// Create Custom Blends Asset:
// Assets > Create > Cinemachine > Cinemachine Blender Settings

// In Cinemachine Brain:
// - Custom Blends = your asset
// - Configure blend times per camera pair

摄像机震动

脉冲源(触发震动)

using Unity.Cinemachine;

public class ExplosionShake : MonoBehaviour {
    public CinemachineImpulseSource impulseSource;

    void Explode() {
        // Trigger camera shake
        impulseSource.GenerateImpulse();
    }
}

脉冲监听器(接收震动)

// Add to CinemachineCamera:
// Add Component > CinemachineImpulseListener

// Impulse listener automatically receives shake from nearby Impulse Sources

自由视角摄像机(第三人称鼠标视角)

Cinemachine Free Look

// GameObject > Cinemachine > Cinemachine Free Look

// Creates 3 rigs (Top, Middle, Bottom) that blend based on vertical input
// Configure:
// - Orbit Radius: Distance from target
// - Height Offset: Camera height at each rig
// - X/Y Axis: Mouse or joystick input

状态驱动相机(基于 Animator)

Cinemachine 状态驱动相机

// GameObject > Cinemachine > Cinemachine State-Driven Camera

// Configure:
// - Animated Target: Character with Animator
// - Layer: Animator layer to track
// - State: Assign camera per animation state (Idle, Run, Jump, etc.)

// Camera automatically switches based on animation state

Dolly 轨道(过场动画)

Cinemachine Dolly Track

// 1. Create Spline: GameObject > Cinemachine > Cinemachine Spline

// 2. Create Dolly Camera:
//    GameObject > Cinemachine > Cinemachine Camera
//    Body > Spline Dolly
//    Assign Spline

// 3. Animate dolly position on spline (Timeline or script)

Common Patterns

第三人称跟随相机

// CinemachineCamera
// - Follow: Player Transform
// - Body: 3rd Person Follow (shoulder offset, distance: 5)
// - Aim: Composer (frame player at center)

瞄准相机(放大)

// Normal Camera (Priority 10):
//   - Distance: 5.0

// Aim Camera (Priority 5):
//   - Distance: 2.0
//   - FOV: Narrower

// Script:
void StartAiming() {
    aimCamera.Priority = 15; // Blend to aim camera
}

过场动画相机序列

// Use Timeline:
// 1. Create Timeline (Assets > Create > Timeline)
// 2. Add Cinemachine Track
// 3. Add virtual cameras as clips
// 4. Timeline automatically blends between cameras

从 Cinemachine 2.x 迁移(Unity 2021)

API 变更(Unity 6 / Cinemachine 3.0)

// ❌ OLD (Cinemachine 2.x):
CinemachineVirtualCamera vcam;
vcam.m_Follow = target;

// ✅ NEW (Cinemachine 3.0+):
CinemachineCamera vcam;
vcam.Follow = target; // Cleaner API

主要变更:

  • CinemachineVirtualCamera → CinemachineCamera
  • m_Follow, m_LookAt → Follow, LookAt(不再使用 "m_" 前缀)
  • 组件已重命名,以提高清晰度
  • 性能更佳

Performance Tips

  • 限制活动虚拟相机的数量(仅在需要时启用)
  • 使用低优先级相机,而不是销毁/创建相机
  • 当远离玩家时,禁用虚拟相机

调试

Cinemachine 调试

// Window > Analysis > Cinemachine Debugger
// Shows active camera, blend info, shot quality

来源

  • https://docs.unity3d.com/Packages/com.unity.cinemachine@3.0/manual/index.html
  • https://learn.unity.com/tutorial/cinemachine

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 cl/Claude-Code-Game-Studios