Moving camera with ios drag (similar to UIScrollview)

Hi, I’m new to Unity. I’m trying to move the camera relative to the touch drag with a fixed Z and Y. Simulating the basic movement of a kind of UIScrollView.

I’ve played around with a few different ideas here and none are panning out. I’ve been using Input.touches[0], storing the x during the Began phase, then calculating the difference during the Moved event. What I don’t understand is converting the screen coordinates to object coordinates. My feeling is that it has something to do with ScreenToViewportPoint or something along those lines, then using the Camera.main.transform.Translate.


I’m not sure if my implementation is normal practice, but it has worked so far for what I am doing. When taking into consideration iOS scrolling movement, there are actually quite a few things that you need to look out for. First, is moving the screen with the position of your finger. Second is taking your ‘dragged’ information and converting it into velocity of some sort for your camera. Third is the bounds of your screen and how you want your camera to react when it gets to those bounds.

The reason I say I don’t know if my implementation is because I’m not using any kind of standardized way to calculate screen movement. I’m basically just taking the deltas of the touch.position and converting that directly into movement for my camera with a speed modifier for the input data. Here’s some basic stuff that hopefully will help you in c#:

Touch touch = Input.touches[0];

switch ( touch.phase )
     case TouchPhase.Began:
          isSwipe = true;
          startPos = touch.position;
          startTime = Time.time;

     case TouchPhase.Moved:
          translate -= new Vector3( ( touch.deltaPosition.x / ( touchModifier ) * Time.deltaTime, 0, 0 );
          camera.transform.position += translate;

     case TouchPhase.Ended:
          if ( isSwipe )
               "do camera velocity here"

“touchModifier” is a variable that is declared so I can change depending on what screen size and platform I’m on, which I do a check for in Start(), and “isSwipe” I think is self explanatory.

For the ‘velocity’ add to the camera, I used iTween for my implementation, utilizing iTween.MoveBy(). I basically calculate the Vector3.magnitude of the swipe on screen and translate that into a MoveBy(), and utilize several of the iTween hash tag arguments to limit the movement and control it with other functions.

Hopefully this will get you started on the right path, or at least some kind of path =p.