Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Positional Lock Extension Conflicts with Confiner

Discussion in 'Cinemachine' started by Xenon27, May 26, 2023.

  1. Xenon27

    Xenon27

    Joined:
    Oct 13, 2020
    Posts:
    3
    I've been using a modified version of this example script which locks a vcam to a particular axis taken from this thread
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3. [ExecuteInEditMode]
    4. [SaveDuringPlay]
    5.  
    6. public class CinemachineCameraLock : CinemachineExtension{
    7.  
    8.     public bool lockX;
    9.     public bool lockY;
    10.     public float x;
    11.     public float y;
    12.  
    13.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime){
    14.         if (stage == CinemachineCore.Stage.Body){
    15.             var pos = state.RawPosition;
    16.             if(lockX){
    17.                 pos.x = x;
    18.             }
    19.             if(lockY){
    20.                 pos.y = y;
    21.             }
    22.             state.RawPosition = pos;
    23.         }
    24.     }
    25. }
    This does work, but I've noticed that sometimes the camera starts to move in a sort of inverted way relative to the target in the directions that are supposed to be fixed. Removing this component from my vcam and re-adding it fixes this issue temporarily, but it always returns as soon as the scene or any others using this are reloaded, or if I close and re-open the project. I've created a simple test project to try and narrow down the issue, and I've discovered that this issue only occurs when there is also a confiner2D component attached to the camera as well. Issue seems consistent between scene and game.

    I imagine some workaround is possible by further modifying the CinemachineCameraLock script so that I can pass in bounds for axis that are not being locked, and then not use a confiner at all, but it would certainly be preferable if I could use confiners instead.

    I've attached a zip of my test project that should demonstrate this issue, if it is of any help.
     

    Attached Files:

  2. Xenon27

    Xenon27

    Joined:
    Oct 13, 2020
    Posts:
    3
    As a note to anyone in the distant future who encounters this problem and its absence of a solution, I did have success removing the confiner entirely, and enforcing limits on the vcam in the script. I simply pass minimum and maximum values for the x and y coordinate through the inspector, and then check if the position of the vcam is within the bounds and move it there if it's not. Works fine for my purposes where I would only be using perfectly rectangular camera bounds, but might take some additional math to allow for other shapes.