Locking Camera Rotation with Cinemachine Position Constraints
In this lesson, you will learn how to use Cinemachine Position Constraints to force your Virtual Camera to maintain a fixed relative rotation regardless of its target's chaotic movement.
The Problem of Target Rotation
When you use a standard Look At target for a Virtual Camera, the camera constantly rotates to track that object. While this is great for fluid movement, sometimes you need a "locked" perspective—for example, a side-scrolling platformer or a top-down view where the camera must ignore the target’s specific rotation (like a character turning around) and maintain a consistent, stable frame.
Implementing the Constraint
Instead of letting the Look At property handle the orientation, we use a Cinemachine Position Constraint. This component allows you to anchor the camera's position relative to a target while explicitly defining a constant rotation offset.
By adding a CinemachinePositionConstraint extension to your Virtual Camera, you can set the Maintain Offset property. When enabled, the camera locks its position relative to the target's origin point at the moment of activation. To prevent the camera from inheriting the rotation of the target (which would cause the view to spin as the character turns), you configure the constraint to ignore the target’s rotation axis.
You can set this up directly in the Inspector:
- Select your
Virtual Camera. - Add the
CinemachinePositionConstraintextension. - Assign your target to the Target field.
- Set the Update Method to
LateUpdateto ensure the camera tracks after the character’s movement script has finished. - In the Rotation Offset field, define your desired permanent angle (e.g.,
0, 0, 0for a perfectly locked side-on view).
By decoupling the rotation from the target's transformation, you ensure that even if your character does a 180-degree flip, your camera remains perfectly steady, providing a professional, polished cinematic feel to your game’s perspective.
Key Takeaway: Use Cinemachine Position Constraints to override dynamic Look At behaviors, allowing you to maintain a static, locked orientation while still keeping the camera anchored to a moving subject.