- Home /
Drag camera with two finger swipe
Hello!
I'm trying to make the camera to be dragged by placing two fingers on the screen and swiping them up/down - left/right, but with no success by now. I've made it draggable with one finger, but am still struggling to make it move with two-finger swipe.
Here's my approach:
public float dragSpeed = 10;
private Vector2 dragOrigin;
private Vector2 startPoint;
void Update()
{
if (Input.touchCount > 1){
dragOrigin = GetInputRange();//Input.GetTouch(0).position;
return;
}else{
return;
}
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position - dragOrigin);
Vector3 move = new Vector3(-pos.x, -pos.y, 0);
transform.Translate(move, Space.World);
}
static bool IsBegun(Touch touch1, Touch touch2)
{
return (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Began) ||
(touch1.phase == TouchPhase.Stationary && touch2.phase == TouchPhase.Began) ||
(touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Began);
}
Vector2 GetMidpoint(Touch touch1, Touch touch2)
{
return Vector2.Lerp (touch1.position, touch2.position, 0.5f);
}
Vector2 GetInputRange()
{
if(Input.touchCount < 2)
return Vector2.zero;
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
var midpoint = GetMidpoint(touch1, touch2);
if(IsBegun(touch1, touch2))
startPoint = midpoint;
return startPoint;
}
I'd be grateful if someone could land a hand. Thank you :)
Would help if you said what was wrong with it. But, Update can't reach line 14 (you should be seeing warnings about that.)
Then, just for ease of reading, IsBegun seems redundant -- could just check whether touch2 has begun.
There is a great resource on the Unity Wiki.
Tap to $$anonymous$$ove Drag to Look iPhone : http://wiki.unity3d.com/index.php/Tap_to_$$anonymous$$ove_Drag_to_Look_iPhone
and : http://wiki.unity3d.com/index.php/Finger$$anonymous$$anager
Answer by ScottYann · Aug 04, 2013 at 12:01 AM
I don't want to sound like an EasyTouch Shill (how do you know I'm not?) but it makes touch control so much easier that trying to do it with unity alone. There are also other touch managers in the asset store that might work just as well..
It was worth what ever I paid for it.