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

Cinemachine Confiner confines the MainCamera but not the VirtualCamera

Discussion in 'Cinemachine' started by robrab2000-aa, Mar 9, 2020.

  1. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    Hi! I was wondering if anyone knew if there was a way to constrain a virtual camera within a mesh? By using the constraint extension I can get the actual main camera to be constrained however the virtual camera object is allowed to move through the collider. I'm allowing the user to move the virtual camera and so what ends up happening is that if (for instance) they move the vcam down through the floor and continue instructing the camera to go down for another 2 seconds, then when they want to move the camera back up again, they need to hold down the up button for 2 seconds before the vcam lined back up with the main camera.
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hi, we don't have that feature built-in.
    First we position the vcam (raw position). Then we apply confiner (and possibly other corrections), which calculates a correction vector for the vcam position. Then the main camera is set to vcam raw position + correction.

    You could try scripting something for your use case.
    If you have a vcam, then you can access CameraState through the State property. From the State property, you'll have access to RawPosition(), PositionCorrection(), and FinalPosition(), which return "the raw position", "the correction vector", and "raw position + correction vector" respectively.
    From these, it may be possible to deduce when the user input can be ignored, because it's just pushing the vcam below your floor.
    Note:
    Your main camera will be at RawPosition() + PositionCorrection() or equivalently at FinalPosition().
    Your vcam will be at RawPosition().
     
    ShervinM likes this.
  3. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    Thanks very much :) I will give this a go!
     
    gaborkb likes this.
  4. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    This worked perfectly, thanks!

    All I had to do was move the currentl vCam to the Corrected position when CameraWasDisplaced() returned true.
     
    ShervinM and gaborkb like this.