LockCameraY - Cinemachine Extension for Unity
A lightweight, efficient Unity script that constrains a Cinemachine Virtual Camera to a fixed vertical (Y) position.
This extension is ideal for 2D platformers, side-scrollers, or top-down games where you want the camera to follow a player horizontally but stay at a consistent height, regardless of jumps or falls.
๐ Features
Axis Locking: Locks the Y axis while allowing the Cinemachine Body (Transposer, Framing Transposer, etc.) to handle X and Z movement.
Pipeline Integrated: Operates within the PostPipelineStageCallback to ensure the lock happens after physics calculations, preventing camera jitter.
Inspector Friendly: Easily adjust the Locked Y Position directly in the Unity Editor.
Performance Optimized: Minimal overhead, utilizing the native Cinemachine extension system.
๐ Installation
- Ensure you have the Cinemachine package installed via the Unity Package Manager.
- Download 'LockCameraY.cs' and drop it into your project's Assets folder.
- Ensure the file name matches the class name: 'LockCameraY.cs'.
๐ How to Use
- Select your Cinemachine Virtual Camera in the Hierarchy.
- In the Inspector, scroll down to the Extensions section.
- Click the Add Extension dropdown and select LockCameraY.
- Set your desired height in the Locked Y Position field.
๐ Requirements
Unity
Cinemachine: 3.0+ (Uses the Unity.Cinemachine namespace)