Search Unity

Camera Panning in 2D with Cinemachine

Discussion in 'Cinemachine' started by arvzg, Nov 17, 2017.

  1. arvzg

    arvzg

    Joined:
    Jun 28, 2009
    Posts:
    619
    Hi guys

    I'm working on a top-down 2D game, and am trying to implement Cinemachine into the game. Previously I had a script which follows the character, but allows you to pan the camera around by holding the left mouse button and moving the cursor. When you let go of the mouse button, the camera tweens back to the character's position.

    How would you implement something like this with Cinemachine? While Cinemachine Brain is active the camera transforms cannot be moved manually, so it appears I'd need to do the panning through Cinemachine, but I can't see how I would do this.

    Thanks!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    You would create a virtual camera that is controlled by the mouse position (set the Aim and Body both to "do nothing" and you can control its position/orientation manually). When the mouse button is pressed, enable this vcam; when it is released, disable it. Cinemachine will bend to/from this manual vcam according to your blend settings.
     
  3. arvzg

    arvzg

    Joined:
    Jun 28, 2009
    Posts:
    619
    Thanks @Gregoryl this seems like it'll work nicely.

    I've got a 'Character VC' set to priority 10, and a 'Pan VC' set to priority 11. Blending between them seems to work if I keep them seperate, but if I put Pan VC as a child of Character VC then Pan VC never goes Live even when I enable it.

    Is this a bug or is parenting cinemachines just not designed to work?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Parenting vcams has a special meaning in Cinemachine. In this case, as you have discovered, the child vcam will be hidden from the Brain and won't ever go live. Keep them separate. Parenting is used for complex "manager-style" vcams like ClearShot, MixingCamera, and StateDrivenCamera.
     
  5. swanijam

    swanijam

    Joined:
    Nov 14, 2016
    Posts:
    23
    Just in case anybody else is looking for this functionality in the future, I found having a second virtual cam match up nicely with the freelook cam pretty difficult, and even more difficult if you want camera panning to have momentum as with the freelook camera, or for the camera to have noise.

    This is a much simpler solution that allows you to have just 1 Cinemachine FreeLook camera.
    You can just copypaste the code in the fourth comment and it'll work like a charm :)