Cinemachine概述

Cinemachine is a suite of modules for controlling the Unity camera. Cinemachine solves the complex mathematics and logic of tracking targets, composing, blending, and cutting between shots. It is designed to significantly reduce the number of time-consuming manual manipulations and script revisions that take place during development.

a suite of:一组\一套

composing:构图

tracking targets:追踪目标

cutting:裁剪

shots:镜头

Time-consuming:耗时的

Manipulations:操作

Revisions:修改

take place:发生

这句话的意思就是Cinemachine能够帮助我们提高开发效率。

The procedural nature of these modules makes Cinemachine robust and forgiving. When you make adjustments - for example, change an animation, vehicle speed, terrain, or other GameObjects in your Scene - Cinemachine dynamically adjusts its behavior to make the best shot. There is no need, for example, to re-write camera scripts just because a character turns left instead of right.

nature:本质

robust:健壮的、强大的

forgiving:容错的

terrain:地形

There is no need:没有必要

这句话的意思:Cinemachine的功能很强大,能够根据场景中各种元素的变化自动调整摄像机的行为,从而减少了开发者的工作负担,并提高了开发效率。

Cinemachine works in real time across all genres including FPS, third person, 2D, side-scroller, top down, and RTS. It supports as many shots in your Scene as you need. Its modular system lets you compose sophisticated behaviors.

in real time:实时

across:遍及

side-scroller:横版通关游戏

top down:俯视角游戏

modular:模块化的

as many as...:和...一样多

快速开始

Get the essential information to understand how Cinemachine works and follow instructions to set up the minimum functional layers to start using Cinemachine in your project.

follow:按照

instructions:指示、说明

set up:设置、安装

minimum:最小的

functional:功能性的

这里强调:只需要配置最基础的部分就可以开始使用,而不是一次性完成所有的高级设置

3个核心元素

Unity Camera:Unity摄像机是实际负责捕捉场景图像的游戏对象,而Cinemachine摄像机通过附加的组件来管理和控制这些Unity摄像机的行为。每个Cinemachine设置只需要一个Unity摄像机,但可以有多个Cinemachine摄像机来实现不同的拍摄效果和控制逻辑

The Unity Camera is a GameObject that includes a Camera component, as opposed to Cinemachine Cameras, which includes other types of components to control the Unity Camera.

as opposed to:与之相对的是,xxx

A Cinemachine setup must include only one Unity Camera, which is then the only GameObject that captures the images from your Scene while the Cinemachine Cameras control it.

Cinemachine Brain

负责管理Cinemachine相机的切换逻辑,它根据相机的 Priority(优先级)决定哪个相机成为 Live 状态。

摄像机的控制与过渡

多个虚拟相机(Virtual Cameras)通过 优先级(Priority)竞争控制权。

高优先级的相机会“接管” Unity 主相机(Unity Camera)。

// 假设有两个虚拟相机
public CinemachineVirtualCamera cameraA;
public CinemachineVirtualCamera cameraB;

void SwitchCamera() {
    cameraA.Priority = 10;  // 激活 cameraA
    cameraB.Priority = 5;   // 停用 cameraB
}

Cinemachine Camera states

虚拟摄像机有三种不同的状态

  1. Live:控制Unity相机,优先级(Priority)最高。混合时,两个虚拟相机都处于Live。混合结束后,只有一个处于Live。

  2. Standby:监控优先级,当优先级超过当前Live相机时触发切换。

  3. Disabled:不参与优先级竞争,需通过 SetActive(false)Priority = 0 设置。

// 动态切换优先级  
void SwitchLiveCamera(CinemachineVirtualCamera newLiveCamera) {  
    // 将所有相机的优先级设为默认值  
    foreach (var camera in allCameras) {  
        camera.Priority = 0;  
    }  

    // 设置新相机的优先级为最高  
    newLiveCamera.Priority = 10;  
    Debug.Log("激活相机已切换为:" + newLiveCamera.name);  
}  

Cinemachine Camera transitions

两种切换模式:

  • Blend(混合):平滑动画过渡(如淡入淡出)。

  • Cut(硬切):瞬间切换。

虚拟相机

虚拟相机就是一个空对象(我理解为一个配置)

当我们在虚拟相机中配置完相关属性后,就会将这些配置应用到主摄像机上。

比如:当我们修改修改虚拟相机的位置时,其实修改的是主摄像机的位置。

那么为什么会有一个虚拟相机的概念?

我们为什么不直接修改主摄像机的配置呢?

这是因为当我们想要实现相机的平滑过渡、切换,就比较复杂。

我们可以使用两个虚拟相机,主摄像机从一个虚拟相机去映射到另外一个虚拟相机中。就可以很容易的思想两个视角的切换。

简单跟随

我们可以通过设置虚拟相机的Follow(跟随目标),就可以实现简单的跟随

复杂跟随

死区;在这个区域内,角色可以移动,但是摄像机不会跟随。

2D游戏中,死区一般设置在宽度0.2,高度0.1的范围内即可

阻尼:通过设置阻尼来平滑相机的移动

有一个黄色标记的追踪点,