Search Unity

Advice on 2D touch controlled camera setup

Discussion in 'Cinemachine' started by PythagoRascal, Sep 30, 2019.

  1. PythagoRascal

    PythagoRascal

    Joined:
    Oct 25, 2012
    Posts:
    26
    Hi all,

    I have a 2D project I'm working on and I'm trying to create a setup like the one I illustrated below.

    End Goal

    I have a scrolling Main VCam that should be controlled through touch input (scrolling only on X axis). This camera is confined in a (possibly dynamic width) confinement zone. When the camera reaches either end of the confinement zone, I would like to have a pull-to-refresh-like behaviour where the scrolling is dampened and springs back if released before a threshold is reached. If the threshold is reached, I want to detect that and trigger a transition to the respective VCams on either the left or right. The same should work backwards.

    Additionally, I have some Detail VCams which are more zoomed in. These can be transitioned to via UI. When zoomed in, no scrolling should be happening on the Main VCam.

    camera_diagram.png

    What I need advice on

    How do I reconcile inertial scrolling, confining & dampening at the same time?

    The way I implemented the camera scrolling is by writing a custom transposer, but I failed at reconciling the scrolling of the camera with the confining and dampening needed for the transitions. Specifically, my setup breaks apart when I still have inertia from the scrolling but also dampening from reaching those zones. I also haven't figured out how to set up the transition threshold from inside a custom transposer.

    Alternatively, I also tried implement the scrolling by moving a Follow target, but again failed to correctly handle the confining & dampening of said Follow target when reaching the dampening/transition zones. In turn, handling the transition threshold is as easy as using some trigger colliders.

    How would you approach such a setup? I'm not averse to writing some custom CinemachineExtension or CinemachineComponentBase to make this work. I just tried to not have to write my own confiner since I don't know the math involved and don't know if I'd understand it.

    Thanks in advance for your help!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Thanks for the detailed post, but I'm still having some trouble understanding exactly what you're trying to do and where it's going wrong.

    Can you explain what's different between the end vcams and the main vcam? Why can't you just have the main vcam stop when it reaches the end?

    Also, why do you have a custom transposer? What does it do that's different from the built-in transposer?
     
  3. PythagoRascal

    PythagoRascal

    Joined:
    Oct 25, 2012
    Posts:
    26
    Thanks for your response, I'll try to clarify.

    In our current prototype we have all the different areas as separate static screens in separate scenes. In an effort to make the UI/UX a bit more dynamic we're trying bring them into one scene where the player can smoothly transition between them.

    In the context of the game, the Main VCam is for showing an overview of the world/different areas of that world. The end VCams show management screens where the player can decide how to use their resources. They have mostly different UI. The Detail VCams show a specific area of the world more zoomed in and with context specific UI.

    This is probably a lack of understanding on my part about how to work with Cinemachine. The interaction from the player that triggers a transition from Main VCam to one of the end VCams is either 1) the press of a UI button from anywhere or 2) a swipe when at one of the ends of the confinement zone. In the case of the swipe, I don't want the transition to trigger immediately but only when swiping a certain distance over the end of the confinement zone. From my limited understanding I assumed this would be done with the dampening setting on the CinemachineConfiner.

    This probably also comes from a lack of understanding in how to work with cinemachine. I didn't know how I would best move the camera through touch input (swiping). I saw that you can write a custom transposer through extending CinemachineComponentBase and did so to hook in my swipe/touch input detection. Another reason is that I didn't know how I would handle confinement of the camera if I controlled the position of the VCam via Follow target, since the Follow target could move outside of the confinement zone (and how I would back-sync the position to the Follow target from the dampening done by the confiner). If there is a better solution I'd be happy to scrap that custom transposer :).
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Leaving the detail vcams aside for the moment, I'd like to see what happens if you try again to implement the main vcam, using only out-of-the-box CM components.
    1. Create an invisible follow target, which will represent your look-at point. Add a custom script that moves it from side to side, in response to the player's swipe action. Don't put any damping or inertia on it, just move it to the new spot instantaneously. Clamp it so that it will never make the camera move horizontally beyond the end zones of your playing area (i.e. don't allow it to move farther than the center of either end zone).
    2. Make a vcam with a Framing Transposer in the body, DoNothing in the Aim, and use the invisible object as the follow target.
      • Put no confiner on the vcam.
      • Put no dead zone on the vcam, just a soft zone
      • Set the damping so that the vcam follows the target smoothly.
    3. When the main vcam is in the end zone (you can set up a trigger to detect this), put up your special end-zone UI.
    What's wrong with that setup?
     
  5. PythagoRascal

    PythagoRascal

    Joined:
    Oct 25, 2012
    Posts:
    26
    Thank you, I'll try that setup for now and try to build the inertia and dampening on top of that.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    The X Damping in the Framing Transposer should be enough to handle inertia and damping. You shouldn't have to build anything else. Just set the float value and the soft zones to give you the effect you want.