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
1
Question by inihility · Oct 06, 2015 at 12:50 PM · androidtouchmultitouchtouchestouching

Unusual multitouch behavior (Android)

Hi, I'm hoping someone can help shed some light on this problem I've been trying to solve over the past couple of days with no luck.

So the idea is that I'm trying to detect multiple touches on an Android device with the purpose being for each touch to move its' own gameobject. The problem I am encountering is that when I have 2 touches, and the 1st touch ends/cancels, the 2nd touch also seems to stop with it, what's weirder is that as soon as I initiate another (originally 1st) touch, the 2nd touch that did not end will suddenly continue to move the gameobject just fine.

This is a rough/simplified representation of the code I have:

 public class TrackerHandlerScript : MonoBehaviour {
     
     public static GameObject[] trackerArray = new GameObject[2];
 
     private int _tapCount;
     
     private Object trackerPrefab;
     // Use this for initialization
     void Start () {
         trackerPrefab = Resources.Load("Prefabs/prefab_tracker");
     }
     
     // Update is called once per frame
     void Update () {
         _tapCount = Input.touchCount;
         if(_tapCount > 0 ){
             for(int i = 0; i < _tapCount; i++){
                 if(Input.GetTouch(i).phase == TouchPhase.Began){
                     createTracker(i, Input.GetTouch(i).fingerId);
                 }
             }
         }
     }
     
     void createTracker(int idx, int id = 0){
        if(trackerArray[idx] != null){
               Destroy(trackerArray[idx] .gameObject);
        }
        trackerArray[idx] = Instantiate(trackerPrefab, Vector3.zero, Quaternion.identity) as GameObject;
        trackerArray[idx] .GetComponent<TrackerScript>().touch = id;
     }
 }

The idea here is that I store my 2 gameobjects in an array and only instantiate a tracker into an empty index. This seems to be working as intended.

This is the logic in the gameobject's script that handles the touch:

 public class TrackerScript : MonoBehaviour {
     public int idx;
     public int touch;
     Vector3 pos;
     void Update () {
         for(int i = 0; i < Input.touchCount; i++){
             if(Input.GetTouch(i).fingerId == touch){
                 if(Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled){
                     Destroy(gameObject);
                 }
             }
         }
         for(int i = 0; i < Input.touchCount; i++){
             if(Input.GetTouch(i).fingerId == touch){
                 pos = Input.GetTouch(i).position;
             }
         }
         _transform.position = Camera.main.ScreenToWorldPoint(pos)
     }
 }

So no syntax errors or anything (in case I made a typo or left anything out). The idea here is that I use the passed fingerId from the handler to identify the touch and only move the gameobject if it's a match. Releasing the touch will destroy the gameobject thus nullifying the index for the tracker array.

Once again, if anyone has any idea what might be wrong with this code/logic, I'm all ears.

P.S. I am on Unity 4.6.3f for full disclosure.

edit: updated some of the code.

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

3 Replies

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

Answer by inihility · Oct 11, 2015 at 10:54 AM

I managed to resolve the issue.

For anyone who's curious, it is mainly with how Unity handles touches, I had to centralize what I did with the touches all in one place in order to maintain the correct touch indexes.

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 hexagonius · Oct 10, 2015 at 04:59 PM

the second stops because your for loop iterates only as often as fingers on the screen, leaving touch 2 out after you removed whatever finger. you must not bind the finger id to their initial touch index, but check them all against however many touched you have.

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 inihility · Oct 11, 2015 at 03:01 AM 0
Share

I've already submitted a comment (pending?) that I solved my problem, but this is exactly what I had to do; check touches against the touch count and do stuff there ins$$anonymous$$d of trying to 'bind' a touch to an index.

avatar image
0

Answer by s4shrish · Oct 22, 2019 at 07:17 AM

Yeah, just printed a long list of debug.logs to figure out the issue myself. Unity handles touch in a weird manner.

So if put your first finger, say on a blue object, it will have touchIndex (in Input.GetTouch(index)) of "0" and then if you put your second finger, say on an orange obj, it will have a touch index of "1". Now obviously when you lift any of the finger, whether 0 or 1 (blue or orange), the remaining touch will have the index of "0", which is not what I initially expected, but makes sense in an array. Fair enough.

So if the finger you lifted from the screen was your second finger (ie the one on orange obj) and put it back on the screen, it will have once again touch index of "1". But if you lifted your first finger (ie the one on blue obj), and put it back on, you would expect the new finger to have the index of "1", but it will have an index of "0" in the array, and the finger on orange will have index of "1", even tho when it was single touch it had an index of "0", it will be changed. THIS caused my objects swap positions quickly when I lifted my finger from one object. But not if it was second finger that was being lifted. Took me quite some time to figure it out, maybe next time I will use fingerID, or heck make alternate script that uses fingerID. Correct me tho if this is not how it works.

Just putting my encounter with an issue out there if someone had this issue with Unity's touches and didn't know how touches function in multi-touch situation in Unity.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Touch.Phase Differences ? 2 Answers

Merging touches 0 Answers

¿Unity messing up fingerIDs? [Android] 1 Answer

Detect Android device touch points amount. 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