Search Unity

Question Cinemachine virtual camera going out of collision bounds

Discussion in 'Cinemachine' started by KikasPanos, Nov 14, 2022.

  1. KikasPanos

    KikasPanos

    Joined:
    Sep 2, 2022
    Posts:
    1
    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
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    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?
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    5. public class ApplyCorrections : CinemachineExtension
    6. {
    7.     protected override void PostPipelineStageCallback(
    8.         CinemachineVirtualCameraBase vcam,
    9.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    10.     {
    11.         if (stage == CinemachineCore.Stage.Finalize)
    12.         {
    13.             state.RawPosition = state.CorrectedPosition;
    14.             state.PositionCorrection = Vector3.zero;
    15.             state.RawOrientation = state.CorrectedOrientation;
    16.             state.OrientationCorrection = Quaternion.identity;
    17.         }
    18.     }
    19. }
    20.  
     
    abhimanyu-singh likes this.
  4. abhimanyu-singh

    abhimanyu-singh

    Joined:
    May 24, 2016
    Posts:
    11