Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by pretender · Sep 01, 2011 at 02:26 PM · androidtouchmultitouch

question about touches for iOS/Android

i recently started porting our app to android tablet and i have some problems with touches.

basically we have camera that rotates around object, left click for selecting objects, and multitouch for zooming.

it is easy to identify one from two touches, but how to register that i, for example, started rotating camera and i do not want any selection? i hope i am clear enough.

currently now if i am rotating camera, if any object is below my finger it gets selected, this is not what i want. it was easier to solve this problem with mouse clicks.

any help appreciated.

[EDIT] here is the code i have so far:

     //COUNT HOW MANY TOUCHES WE HAVE
     var count = Input.touchCount;
 
     //IF WE HAVE AT LEAST ONE
     if (count == 1)
     {
             //GET TOUCH STRUCT FOR EACH
             var touch : Touch = Input.GetTouch(0);
             
             //DETERMINE THE PHASE OF EACH TOUCH
 
                     //WHEN TOUCH BEGAN
             if (touch.phase == TouchPhase.Began)
             {
                 
             }
             
             //WHEN TOUCH IS MOVED
             if(touch.phase == TouchPhase.Moved){                
                 
                    // Get movement of the finger since last frame
                    touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                    
                     x += touchDeltaPosition.x * xSpeed*0.01; 
                     y -= touchDeltaPosition.y * ySpeed*0.01; 
                     
             
             }
 
             
     //FOR ZOOMING IF WE HAVE MULTITOUCH
     }else if(count==2){
     
         //IF WE HAVE MOVEMENT OF BOTH TOUCHES PERFORM ZOOM
         if(Input.GetTouch(0).phase==TouchPhase.Moved && Input.GetTouch(1).phase==TouchPhase.Moved){
             
             distance += Input.GetTouch(0).deltaPosition.y*0.1;
         
         }
     }
Comment
Add comment · Show 2
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 DaveA · Sep 01, 2011 at 03:33 PM 0
Share

I can help if you can post your code

avatar image pretender · Sep 01, 2011 at 03:40 PM 0
Share

ok, i added the code i have so far. it is not much since i do not know how to register if it is tap or drag... thank you for looking into this!

2 Replies

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

Answer by mcarriere · Sep 09, 2011 at 08:51 PM

Here, I wrote this in C#, so you'll probably have to convert it to JS if you want to keep your code the same:

 using UnityEngine;
 using System.Collections;
 
 public enum MoveMode { None, Rotate, Zoom };

 public class ZoomPan : MonoBehaviour {
 
     private MoveMode currentMode;
     private float lastTouchTime;
     private const float touchTimeBuffer = 1f;
 
     void Start ()
     {
         currentMode = MoveMode.None;
         lastTouchTime = 0;
     }
     
     void Update ()
     {
         Touch touch0, touch1;
         int touchCount = Input.touchCount;
 
         if (currentMode == MoveMode.None)
         {
             if (touchCount == 1)
             {
                 touch0 = Input.GetTouch(0);
 
                 if (touch0.phase == TouchPhase.Began)
                 {
                     lastTouchTime = Time.time;
                 }
                 else if (Time.time - lastTouchTime > touchTimeBuffer)
                 {
                     currentMode = MoveMode.Rotate;
                 }
             }
             else if (touchCount == 2)
             {
                 touch0 = Input.GetTouch(0);
                 touch1 = Input.GetTouch(1);
 
                 currentMode = MoveMode.Zoom;
             }
         }
         else if (currentMode == MoveMode.Rotate)
         {
             if (touchCount == 0)
             {
                 currentMode = MoveMode.None;
             }
             else
             {
                 touch0 = Input.GetTouch(0);

                 Vector2 deltaPosition = touch0.deltaPosition;
 
                 // do whatever you want here with your delta position
             }
         }
         else if (currentMode == MoveMode.Zoom)
         {
             if (touchCount == 0)
             {
                 currentMode = MoveMode.None;
             }
             else if (touchCount >= 2)
             {
                 touch0 = Input.GetTouch(0);
                 touch1 = Input.GetTouch(1);
 
                 float zoomDistance = Vector2.Distance(touch0.deltaPosition, touch1.deltaPosition) * Time.deltaTime;
 
                 // do whatever you want with zoom distance.
             }
         }
     }
 }

A few notes to what I did here: I created an enumeration that defines what "Mode" the game is in. This way, we kinda lock ourselves into the state that the user starts in, and stay there until they take their fingers off of the screen. (Which seems to be what you want)

What this does is it originally sets the game in "MoveMode.None". If you put down two fingers within 1 second, you're set to "Zoom" until you let go of the screen. If you put down one finger and hold it for a second, you're in "Rotate" until you let go of the screen. You can increase or decrease this buffer by changing the constant "touchTimeBuffer" to whatever value in seconds that you want.

While this is what you asked for, you might want to rethink the usage, if you look at other apps (look at google maps, for instance) they will automatically switch from pan to zooming, no matter if you "meant to" or not. I also disregarded if both touchphases are "Moved", because some people pinch by putting one finger down, and moving only one of them.

Good luck, hope this helps!

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
1

Answer by jahroy · Sep 09, 2011 at 11:02 PM

Maybe I'm missing something, but it sounds like you simply want to ignore your "select object" code whenever any of the touches are moving.

Here is one way to do that:

 function tabletSelectObject ()
 {
     /* if any fingers are moving, get outta here */
 
     for ( var thisTouch : Touch in Input.touches ) {
         
         if ( thisTouch.phase == TouchPhase.Moved ) {
             return;
         }
     }
 
     /* code to select objects here */
 }

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

Android: After the third touch all touches get cancelled 1 Answer

Multitouch loses touch even when finger still on screen. 0 Answers

¿Unity messing up fingerIDs? [Android] 1 Answer

Unity Bug, Letting User To touch two UI Buttons as same time (simultaneously) 1 Answer

Move 2 objects at the same time 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