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
1
Question by Jesse_Pixelsmith · Jul 28, 2013 at 02:10 AM · touchmulti-touchfingerid

Asynchronous Multi-Touch help - keeping track of touches and corresponding gameObjects (Android)

I'm trying to get multi-touch functionality incorporated into my game - not something like using two touches to do a unique action like pinch-zooming - but using multiple touches to do multiples of the same type of action, independently.

The problem is keeping track of objects that were touched, when other touches may begin or end during the same period the first touch is made (for example).

I'd been using Input.GetTouch(i) but I've read that "FingerID" is a better way to go as it won't change as touches get added or removed.

I've stripped down my code to the basics of what I'm trying to do. Basically there are several boxes that you can touch, which then generates a waypoint tracker that you can drag around, and when you release it, the box that you touched moves to the tracker's las position.

This works perfectly doing one at a time, but I want to be able to do this simultaneously and independently (so two people could play at the same time without worrying about messing the game up).

If someone could take a look and point me in the right direction I'd appreciate it! I also realize that gameObjects "tracker" and "savedSelection" should probably be arrays of gameObjects, but I'm not sure the best way to handle that, so looking for advice on that as part of the solution.

 using UnityEngine; 
 using System.Collections;
 using System.Collections.Generic;
 
 public class MultiTouchTracking : MonoBehaviour 
 {
     public GameObject trackingIndicator; //prefab tracking indicator
     public GameObject tracker; //instance of tracking indicator
     public GameObject savedSelection; //selected object
     private Vector3 touchPosition;
     
     void Update ()    {
             
         if(Input.touchCount > 0 ) 
         {    
             for(int i=0; i<Input.touchCount; i++)
             {
                 if (Input.GetTouch(i).phase == TouchPhase.Began)
                 {    
                     //Player has touched the screen and will raycast to an object, tagging it as "savedSelection"
                     Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                     RaycastHit hit;
                         
                     if (Physics.Raycast(ray, out hit)) 
                     {
                         savedSelection = hit.collider.gameObject;
                         tracker = Instantiate(trackingIndicator, transform.position, Quaternion.Euler(0,0,0)) as GameObject;    
                     }
                 }
                 
                  if (Input.GetTouch(i).phase == TouchPhase.Moved)
                 {
                     //The tracker will move around as the player drags the finger around the screen
                     touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 0f));
                     tracker.transform.position = new Vector3(touchPosition.x, 0f, touchPosition.z);
                 }
         
                 if (Input.GetTouch(i).phase == TouchPhase.Ended)
                 {
                     //Once the finger is lifted up, we send a message to the game object "savedSelection", telling it to go to the point the tracker is at, then we destroy the tracker
                     savedSelection.SendMessage("MoveToPosition", tracker.transform.position);
                     Destroy(tracker);
                 }
             }
         }
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Jul 28, 2013 at 03:35 AM

I would approach this more from a distributed control perspective than the centralized control scheme you are currently using. I would assume it be better if each tracker knew which touch it needs to follow, and then does. You wouldn't have to care about gameobject arrays or even keeping track of touches, because the trackers themselves handle that. All you need to do is slice up that script of yours a little then attach the Tracker part to the trackerindicator

... slicing code ...

The only thing I could think off the top of my head is how do check if there is a new touch. But I'm sure you can figure that out. I hope my code works without a lot of hassle, here goes:

 public class Tracker
 {
 
     public Touch thisTouch;
     public GameObject savedSelection;
 
     void Update ()
     {
         if (thisTouch.phase == TouchPhase.Moved) {
             //The tracker will move around as the player drags the finger around the screen
             touchPosition = Camera.main.ScreenToWorldPoint (new Vector3 (thisTouch.position.x, thisTouch.position.y, 0f));
             transform.position = new Vector3 (touchPosition.x, 0f, touchPosition.z);
         }
  
         if (thisTouch.phase == TouchPhase.Ended) {
             //Once the finger is lifted up, we send a message to the game object "savedSelection", telling it to go to the point the tracker is at, then we destroy the tracker
             savedSelection.SendMessage ("MoveToPosition", transform.position);
             Destroy (this.gameObject);
         }
     }
 
 }
 
 public class MultitouchTracking{
     
     void Update (){
         /*Somehow determine if there is a new touch, possibly checking if touchCount has increased*/
         if (newTouch == TouchPhase.Began) {
             //Player has touched the screen and will raycast to an object, tagging it as "savedSelection"
             Ray ray = Camera.main.ScreenPointToRay (newTouch.position);
             RaycastHit hit;
  
             if (Physics.Raycast (ray, out hit)) {
                 Tracker tracker = (Tracker)Instantiate (trackingIndicator, newTouch.position, Quaternion.Euler (0, 0, 0)).GetComponent<Tracker>();
                 tracker.savedSelection = hit.collider.gameObject;
                 tracker.thisTouch = newTouch;
             }
         }
     }
 }
Comment
Add comment · Show 3 · 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 Jesse_Pixelsmith · Jul 29, 2013 at 03:23 PM 0
Share

Hmm this seems like a good method, and it seems to be assigning "thisTouch" correctly - but I can't seem to get the Tracker script to pick up on "thisTouch" phases $$anonymous$$oved and Ended.

avatar image Jesse_Pixelsmith · Jul 29, 2013 at 03:30 PM 0
Share

yeah using this code, "thisTouch" seems locked in the phase TouchPhase.Began, even if I pull my finger off. I don't see why though...

avatar image Orion_78 · Jan 19, 2015 at 09:30 AM 0
Share

At each Update Unity realocate the Input, so you can't just save your Input as a variable.

Solution is there: http://forum.unity3d.com/threads/how-to-handle-unity-re-assigning-touches.144536/

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

16 People are following this question.

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

Related Questions

ITouch multi touch with joystick problem 0 Answers

Input.touchCount do not detect multitouch in iOS, but works fine for android 0 Answers

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

I try to Create a DLL for get WM_TOUCH windows message! 1 Answer

¿Unity messing up fingerIDs? [Android] 1 Answer


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