- Home /
How to zoom and pan when you "pinch/swipe across screen" (iPhone)
I know how to zoom in when you hit an input key but how would you zoom when you pinch?
Also how would you pan to the side when you do a two finger swipe?
Answer by spinaljack · Jul 15, 2010 at 09:37 PM
First you check that you have 2 touches using touch count == 2.
To handle pinching you find the distance between the 2 touches looping though all touches each frame to get the touch positions:
http://unity3d.com/support/documentation/ScriptReference/Vector2.Distance.html
And then zoom in or out if the distance changes significantly.
For touch panning you just check that the distance between the two touches doesn't change significantly and then you take the average movement of the two touches as a panning motion.
This video and code from blurst considers handling multi touch reliably:
For pinching wouldn't tou need to check if the angle of the 2 vectors is around 180? How would you do that?
no i mean the difference of the vectors is 180. not where you pinch.
Finding the angle of a line between 2 points is simple trigonometry http://en.wikipedia.org/wiki/Trigonometry (tan(angle) = y/x) But again, you can pinch at any angle, try it on goodgle maps.
Two points define a line segment, which is always 180 degrees sorta by definition. You'd need a third point to serve as the corner of the angle, so to speak.
Answer by 1337GameDev · Nov 19, 2011 at 11:07 PM
here is a script i used for zooming using pinch, the Camera game object is assigned in the inspector after its attached to the camera object.
using UnityEngine;
using System.Collections;
public class CameraZoomPinch : MonoBehaviour
{
public int speed = 4;
public Camera selectedCamera;
public float MINSCALE = 2.0F;
public float MAXSCALE = 5.0F;
public float varianceInDistances = 5.0F;
private float touchDelta = 0.0F;
private Vector2 prevDist = new Vector2(0,0);
private Vector2 curDist = new Vector2(0,0);
private float startAngleBetweenTouches = 0.0F;
private int vertOrHorzOrientation = 0; //this tells if the two fingers to each other are oriented horizontally or vertically, 1 for vertical and -1 for horizontal
private Vector2 midPoint = new Vector2(0,0); //store and use midpoint to check if fingers exceed a limit defined by midpoint for oriantation of fingers
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Began && Input.GetTouch(1).phase == TouchPhase.Began)
{
}
if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
midPoint = new Vector2(((Input.GetTouch(0).position.x + Input.GetTouch(1).position.x)/2), ((Input.GetTouch(0).position.y - Input.GetTouch(1).position.y)/2)); //store midpoint from first touches
curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
touchDelta = curDist.magnitude - prevDist.magnitude;
if ((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x) > (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y))
{
vertOrHorzOrientation = -1;
}
if ((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x) < (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y))
{
vertOrHorzOrientation = 1;
}
if ((touchDelta < 0)) //
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
}
if ((touchDelta > 0))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
}
}
}
}
This code has one flaw, i cannot get it to recognize swiping with two fingers, as it tries to zoom when i do that, whether its swiping vertically, horizontally or diagonally. I am not sure how to modify this to work with swipes :/ (I want it to zoom even if they swipe but pinch in or out while swiping)
Now, if you edit both your answers out of here and post this as a proper question, "give me a call" (or e-mail is fine) and I'll try to answer it - because I did solve quite simply this pinch zoom problem on my end. Sorry for the blackmailing. :P
You want me to "edit my answer out" rather than add to this question, where my script actually applies. Ok i guess since everybody wants everybody to post rather than add to reexisting posts ill do that then. Not my server bandwidth i gotta worry about i guess.
BTW, here is my post i put up about a week ago with this question: http://answers.unity3d.com/questions/183365/c-script-for-unity-camera-detect-difference-betwee.html
Unfortunately, there's way more questions than people willing to give answers, and it has been a lost fight for long time for the few of us trying to help. I'm actually the least guy helping here. Anyway, all you had to do in this whole question was to add that last link on the question itself. Ins$$anonymous$$d of requesting a service like you had paid for it, you could simply say something like: "Can someone please help me with my similar question". Understand this is a free space with people contributing with each other just for the sake of the community. Now, feel free to move around / delete / copy / "waste bandwidth" and make all this looks shorter and simpler. ;)
Answer by 1337GameDev · Nov 20, 2011 at 09:03 PM
The code i used was created from a different snippet i made that detected swioes and rotation (hence the midpoint to detect that) you can remove midpoint variable. I also had max and min scale to determine speed of zoom but it was buggy and would inverse my fov which looks odd. The max and min fov are hardcided, just look (or put in those min and max values) in where it specifies the maximum it can be. Look for the mathf. Clamp declaration to figure out those.
Can you post your rotation code? Do you have any ideas to help detect swipes (and still zoom in and out if they are swiping but pinch as well)
Basically samething I said to sohambliss applies here, except the second paragraph of this post is the comment and the first one is a question. Do it with links and post responsibly, please.
Answer by sohambliss · Nov 20, 2011 at 07:31 PM
I used this script and it works nicely, thank you for posting it. I used it in conjunction with another script that rotates an object via an x-axis swipe.
The compiler does say that the midpoint variable is declared but not used. I also cannot figure out the use for the minscale and maxscale variables as I cannot discern their effect when i change them.
The script however does what i would like. I am hoping the minscale and maxscale somehow control the movement range of the camera but i can't tell how to do that. Controlling the range of the camera via this script would be handy.
thanks again. very helpful.
This post has 2 parts: the first one is a comment that should go under spinaljack's answer and the last one is a question, that should be asked as such. You could even link your question in your comment.
Answer by sohambliss · Nov 21, 2011 at 04:25 PM
I found this somewhere in a forum as a means of translating an object as you touch the screen. I modified it to rotate an object. Simply attach this script to a gameobjct you want to spin. There is also a speed variable.
//
// Moves object according to finger movement on the screen
var speed : float = 0.1; function Update () { if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
// Move object across XY plane transform.Rotate (0,-touchDeltaPosition.x * speed, 0);
}
}