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
2
Question by 1337GameDev · Nov 03, 2011 at 09:56 PM · swipemultitouchlinespinch

two finger swipe vs pinch and zoom problem

I have a problem with code for a camera scene for android. I can pan my camera fine, but once i use two fingers my code confuses a pinch with a two finger swipe. I want to zoom when i pinch, and rotate when i swipe with two fingers, but it always does both if i try to swipe.

I figured if i could draw a line between both points using their coordinates and then translate that line to the left and right of both points (or if points or horizontal to translate it below and above the points) and then check if either finger goes out of these two lines and if it does cancel that swipe possibility.

alt text

The red circles are the touch locations, while the blue lines are the lines drawn to represent the limits of each finger....

How would i do this? i know i can calculate a line using this: http://thesaurus.maths.org/mmkb/entry.html?action=entryById&id=4025 but how would i check the coords against this line?

I'm assuming i would create this line at the begin touch phase, and then check it until the touch cancelled phase. How would i go about doing this?

How would i also take into account if the lines are horizontal or vertical? Finger pinches can be horizontal or vertical.

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
2

Answer by cregox · Nov 22, 2011 at 07:17 PM

It gives me much more pleasure answering this question than your other one, so I can understand why you wanted to answer it yourself so many times. But please, constrain all your enthusiasm into 1 answer only. You can even move some of them as comments into the respective answers you were referring to and simply deleting the repetitive answer claims. Also sorry for insisting so much on keeping it clean.

Anyways, following there are simplified illustrative comment-stripped script for you and the full copy of it under unifycommunity. Right now I can't test it on any device and just want to say for panning I did a lot more of fixes not included here that I hope are only needed on my end for any reason.

SimplifiedCameraOrbit.cs

still building it - sorry - this is where the panning part is, which envolves no Touch controls as you may deduce

DetectTouchMovement.cs

 using UnityEngine;
 using System.Collections;
 
 public class DetectTouchMovement : MonoBehaviour {
     const float pinchTurnRatio = Mathf.PI / 2;
     const float minTurnAngle = 0;
     
     const float pinchRatio = 1;
     const float minPinchDistance = 0;
     
     const float panRatio = 1;
     const float minPanDistance = 0;
     
     static public float turnAngleDelta;
     static public float turnAngle;
 
     static public float pinchDistanceDelta;
     static public float pinchDistance;
     
     static public void Calculate () {
         pinchDistance = pinchDistanceDelta = 0;
         turnAngle = turnAngleDelta = 0;
         
         // if two fingers are touching the screen at the same time ...
         if (Input.touchCount == 2) {
             Touch touch1 = Input.touches[0];
             Touch touch2 = Input.touches[1];
             
             // ... if at least one of them moved ...
             if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) {
                 // ... check the delta distance between them ...
                 pinchDistance = Vector2.Distance(touch1.position, touch2.position);
                 float prevDistance = Vector2.Distance(touch1.position - touch1.deltaPosition,
                                                       touch2.position - touch2.deltaPosition);
                 pinchDistanceDelta = pinchDistance - prevDistance;
                 
                 // ... if it's greater than a minimum threshold, it's a pinch!
                 if (Mathf.Abs(pinchDistanceDelta) > minPinchDistance) {
                     pinchDistanceDelta *= pinchRatio;
                 } else {
                     pinchDistance = pinchDistanceDelta = 0;
                 }
                 
                 // ... or check the delta angle between them ...
                 turnAngle = Angle(touch1.position, touch2.position);
                 float prevTurn = Angle(touch1.position - touch1.deltaPosition,
                                        touch2.position - touch2.deltaPosition);
                 turnAngleDelta = Mathf.DeltaAngle(prevTurn, turnAngle);
                 
                 // ... if it's greater than a minimum threshold, it's a turn!
                 if (Mathf.Abs(turnAngleDelta) > minTurnAngle) {
                     turnAngleDelta *= pinchTurnRatio;
                 } else {
                     turnAngle = turnAngleDelta = 0;
                 }
             }
         }
     }
     
     static private float Angle (Vector2 pos1, Vector2 pos2) {
         Vector2 from = pos2 - pos1;
         Vector2 to = new Vector2(1, 0);
         
         float result = Vector2.Angle( from, to );
         Vector3 cross = Vector3.Cross( from, to );
         
         if (cross.z > 0) {
             result = 360f - result;
         }
         
         return result;
     }
 }

Comment
Add comment · Show 2 · 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 1337GameDev · Nov 23, 2011 at 11:15 PM 0
Share

Thanks for replying, i will keep my questions more organized. The page didnt load right on my tablet so i couldn't comment but just answer, but now i changed the version the browser loads. I looked at the code and do most of that code in my anser, but i will combine my movements into one code script and make it more state based, but i still want to be able to zoom, or rotate if they do those actions at the same time.

avatar image cregox · Nov 24, 2011 at 12:48 PM 0
Share

Well, if you can implement this code you should already be able to see pinch zoo$$anonymous$$g and rotating at same time. Unfortunately panning in itself isn't as straight forward as one might expect, as far as my experience goes, and I'm still struggling with simplifying it without taking the whole day off my job. But please, do check the wiki linked above for how to use DetectTouch$$anonymous$$ovement.cs

avatar image
1

Answer by DaveA · Nov 04, 2011 at 12:32 AM

What I did was detect when there are two touches, and track the distance between them. If the distance decreases, it's a zoom-out; if it increases, it's a zoom in. At the same time, I find the center between the two touches, that's the center of rotation. There's also a small threshold value that has to be exceeded before the action takes place. It's up to the user not to move their fingers apart or together while rotating, and that threshold helps keep them in line, so to speak.

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 jahroy · Nov 04, 2011 at 12:47 AM 0
Share

We do pinch/zoom just like DaveA. We use three fingers to pan...

avatar image DaveA · Nov 04, 2011 at 01:03 AM 0
Share

Same here. 1 finger for 'mouselook'

avatar image cregox · Nov 22, 2011 at 12:49 PM 0
Share

I actually use 2 finger to pan, zoom and rotate just all right.

avatar image 1337GameDev · Nov 22, 2011 at 03:37 PM 0
Share

can u give an example how you did so? (to cawas) i would prefer to keep 1 finger for pan, 2 swipe for rotate, and pinch for zoom.

avatar image 1337GameDev · Nov 22, 2011 at 03:38 PM 0
Share

i have them coded, but they conflict, can you post an example cawas how u made them nit interrupt each other.

Show more comments
avatar image
0

Answer by jahroy · Nov 03, 2011 at 11:01 PM

Would it be possible to simply check if both fingers are moving in the same direction?

If so, you could ignore pinch/zoom input when this is the case.

Unless a pinch is moving perfectly along one of the axes, the two fingers should be moving in the opposite direction both horizontally and vertically, right?

(I guess the answer to this question depends on how you define pinch/zoom)

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
avatar image
0

Answer by 1337GameDev · Nov 05, 2011 at 12:31 AM

Hmm, what stops me from using two fingers and swiping left in the pinch/zoom code? It still tries to zoom out if i use two fingers and swipe left (and assuming up, down and right too). How would i stop this and determine the best way to overcome this error?

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
avatar image
0

Answer by 1337GameDev · Nov 19, 2011 at 10:49 PM

Any people have any ideas on this? I still cannot discern how to check that two fingers are performing a pinch without zooming if i swipe them vertically, horizontally or diagonally.... I want to do this and still be able to zoom if they perform the pinch while swiping.... (it would combo with a rotate action i have set for swiping two fingers)

How would i do this? What values does the .position data member actually contain? Is it a vector 2? Does .deltaPosition of a touch position ever become negative??

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# script for unity camera. Detect difference between two finger swipe and pinch to zoom? 2 Answers

Yield Causing function not to be called on multitouch script 2 Answers

multiTouch problem when fingers close together (Bug?) 1 Answer

How do i press multiple buttons at once in a single swipe 0 Answers

Multitouch GameObject slicing 0 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