Search Unity

Smooth 2D Platformer Camera in Cinemachine with C#?

Discussion in 'Cinemachine' started by TimHellmann, Jul 16, 2017.

  1. TimHellmann

    TimHellmann

    Joined:
    Mar 6, 2011
    Posts:
    66
    Hey guys,

    The wonderful devs from Cinemachine invited me to post my question here which I already asked in Facebook.
    I came across an issue while playing around with Cinemachine.
    I wanted to mimic a Super Mario like camera. Something, I thought, Cinemachine can be perfect for due to its smooth transition and damping abilities.
    However, I cannot seem to achieve a satisfying result as C# does not let me enter some CinemachineVirtualCamera stats.

    What do I want?
    The camera needs to be centered at the 2D player in idle mode (already coded via: using Cinemachine;). When the character moves, the camera should make a smooth transition to an offset to show more of the level in running direction.
    So, I need to constantly swap between a + and - offset, depending on the direction of the player, and back to 0 offset when standing again. (Might also require a dead zone and damping that moves with that offset)
    I added two images to further explain my desire.

    However, I cannot seem to create dynamic (and smooth) offsets like that with its inspector settings and, even more important, I somehow cannot set the Aim / Body variables like Tracked Object Offset dynamically in C#. The code just does not show any options like <CinemachineVirtualCamera>().Composer.TrackedObjectOffset.X or something similar. I also added one image to show which variables I mean.

    I would love to get a reply by the experienced people here. Maybe there is a great and easy solution with Cinemachine I currently do not think of. Thank you so much in advance.
     

    Attached Files:

    Last edited: Jul 16, 2017
  2. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    If I remember correctly, this presentation about sidescroller (2D) cameras was specially good:



    Maybe it can give the Cinemachine team some ideas (if they haven't seen it already). I think very few tweaks are needed to make Cinemachine work great for 2D.
     
    Last edited: Jul 16, 2017
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @AriesT Because Cinemachine is modular and extensible, the API you seek must be slightly more complex. But it's there! There is a scripting example scene that comes with CM where this is illustrated.

    Try something like:
    CinemachineVirtualCamera vcam = ...;
    CinemachineTransposer transposer = vcam.GetCinemachineComponent<CinemachineTransposer>();
    if (transposer != null)
    transposer.m_FollowOffset.x = blabla;​

    I would suggest animating the transposer offset in your case, and not the composer offset. Composer will rotate the camera, transposer will move it.