Search Unity

Orth. Cam: Something like target group but with fixed camera position possible with Cinemachine?

Discussion in 'Cinemachine' started by Sharlatan, Jan 8, 2019.

  1. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Hey everyone!

    Is something like the following easily possible with Cinemachine?

    • Orthographic camera that adjusts it's orthographic size to always fit a group of objects, just like when you use a target group in the "Follow" field and a Framing Transposer on your virtual camera.
    • Contrary to the approach above, where the camera always fits the objects in the target group but also follows the target group around, I'd like the camera to have a fixed position, meaning it would just zoom in and out to fit everything but not move around.
    I tried out some stuff e.g. with invisible objects in the group that have a very high weight, with trying to restrict the camera with a very small confiner, etc. but nothing really produced the result I'm looking for.

    Is there a simple way to do this in Cinemachine?

    It wouldn't be too much work to write a simple script for a "normal" camera that does this. But I really like all the options you have with Cinemachine and would prefer to use that and not reinvent the wheel if possible.

    Thank you very much and have a nice day.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Out of the box, no, but you can easily do it with a little scripting.
    • Use a vcam with Do Nothing in both Aim and Body, which allows you to set its transform.
    • Set the LookAt target to the target group.
    • Write a custom CinemachineExtension that modifies the ortho size in response the the target group's bounding box.
    • You can use CinemachineFollowZoom as a starting point. Copy it, rename it, and modify the code to alter the state.Lens.OrthographicSize field in response to the TargetGroup.BoundingBox. Add the new extension to the vcam via the Extensions dropdown in the vcam inspector.
     
  3. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Thank you very much for getting back to me in such a fast manner and providing me with a good idea of how to achieve the desired camera behavior!

    Unfortunately, I can't try it out right now, because I'm currently working on another piece of code I have to finish. That might take me a few more days. But after, I'll try out your idea and post my findings in this thread. Hopefully, they'll be useful to others as well!

    Thanks again and have a nice day.