Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by MikezNesh · Jul 15, 2010 at 08:42 PM · iphonetouchscreenzoompanpinch

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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:

http://technology.blurst.com/iphone-multi-touch/

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikezNesh · Jul 16, 2010 at 05:35 PM 0
Share

For pinching wouldn't tou need to check if the angle of the 2 vectors is around 180? How would you do that?

avatar image spinaljack · Jul 16, 2010 at 08:07 PM 0
Share

not really? people pinch on all directions

avatar image MikezNesh · Jul 18, 2010 at 01:47 AM 0
Share

no i mean the difference of the vectors is 180. not where you pinch.

avatar image spinaljack · Jul 18, 2010 at 01:55 AM 1
Share

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.

avatar image DaveA · Jun 02, 2011 at 11:09 PM 1
Share

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.

Show more comments
avatar image
2

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)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Nov 21, 2011 at 12:21 PM 1
Share

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

avatar image 1337GameDev · Nov 21, 2011 at 05:52 PM 0
Share

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.

avatar image 1337GameDev · Nov 21, 2011 at 05:54 PM 0
Share

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

avatar image cregox · Nov 21, 2011 at 07:19 PM 0
Share

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. ;)

avatar image
-1

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Nov 21, 2011 at 12:20 PM 1
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Nov 21, 2011 at 12:18 PM 1
Share

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.

avatar image
0

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);

}

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to zoom and pan a gameobject not the whole scene 0 Answers

Panning & Zooming using Multi Touch 1 Answer

Pinch and Zoom functionality on Canvas UI Images 7 Answers

easiest way to do pinch zoom? 5 Answers

restrict camera movement, Camera moves out of bounds! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges