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

How to get the resulting Camera for a VirtualCamera before it's active?

Discussion in 'Cinemachine' started by peterrobot2, Aug 27, 2019.

  1. peterrobot2

    peterrobot2

    Joined:
    Dec 21, 2018
    Posts:
    2
    Hey folks,

    I'm looking at implementing a rudimentary camera filtering system, where from a selection of cameras, assess how well they frame a target object, and then disable/avoid using the other cameras out of that group.

    (For a quick visualisation, imagine a bunch of spectators sitting around a stadium looking inwards at a tennis court match, those are my Cameras looking at the gameplay area I've setup)


    One of these filters is to catch when the object is near the edge of the screen, for something similar in a different project, I used:

    Code (CSharp):
    1. Camera.main.WorldToViewportPoint(target.position)
    to check if the result x/y value was within a certain padding threshold, however when attempting this with the CinemachineVirtualCamera, I can't see a way to get what the Camera will look like before the CinemachineBrain updates.


    Is there a way to get a viewport point from the VirtualCamera before the main camera moves to it? Do I need to look at making a CinemachineExtension of some kind?


    Additionally a couple small quirks with this project is I can't rely on the CinemachineCollider, as it's an open area view so no Colliders to block vision. And second is no aim/body movement, all cameras are fixed in place (hence the need for a slightly smarter filtering system than the ClearShot group).
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I would start by building a custom extension that evaluates shot quality based on where the target is in the frame.

    ClearShot uses the ShotQuality field in the CameraState to choose the next camera. CinemachineCollider has a very basic quality evaluator (it's binary: is the target visible or not?). You just have to replace the CinemachineCollider with a custom extension that computes a more sophisticated quality, based on screen position.

    Look inside CinemachineCollider.cs, and find the IsTargetOffscreen() method. Copy the code as a starting point and modify it to compute a float quality (0 to 1) based on angle with camera forward. Store the quality in cameraState.ShotQuality. ClearShot will then choose the camera based on your custom quality evaluation.
     
  3. peterrobot2

    peterrobot2

    Joined:
    Dec 21, 2018
    Posts:
    2
    Gotcha, thanks for the info.