Hello,
I’m trying to implement an RTS-like camera using Cinemachine and I need it to be restricted to a specific region. For that reason, I’ve added a 2D Confiner extension which limits the position of the main camera. My main issue is that the virtual camera goes out of bounds. Because of this when I pan in one direction and I reach a collision bound and I keep panning, while the main camera stops, the virtual camera keeps moving to that direction and so in order to pan in the other direction I need to move the virtual camera back to the bounds first.
I’ve tried many things including changing the update method of the cinemachine brain and also setting the virtual camera position to the corrected position on every Update and then moving/zooming the camera on LateUpdate. While this keeps the vcam to the bounding box, it also causes it to flicker and the end result is a jittering effect when you’re touching the bounds while you’re moving.
Is there any way to have the vcam be restricted to the bounding box while also making use of the confiner extension?
Thanks
Hi,
That’s strange. The vcam should be restricted.
Could you show your Hierarchy and the inspectors of your Main Camera and components, Virtual camera and components?
The vcam’s transform does not by default reflect the corrected position.
You can use this custom extension to apply the corrections to the transform. Drop it in your project and add it via the vcam’s Extensions dropdown.
using UnityEngine;
using Cinemachine;
[SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
public class ApplyCorrections : CinemachineExtension
{
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
{
if (stage == CinemachineCore.Stage.Finalize)
{
state.RawPosition = state.CorrectedPosition;
state.PositionCorrection = Vector3.zero;
state.RawOrientation = state.CorrectedOrientation;
state.OrientationCorrection = Quaternion.identity;
}
}
}
2 Likes
Thanks @Gregoryl . This Worked for me.
Took me half a day to find this topic and finally got mine to work.
As mention in Cinemachine Camera Going out of bounds we need to put an object in Follow Target for the ApplyCorrections script to work.
But the real question is why wasn’t this one of the setting for the cinemachine virtual camera ?