Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
3
Question by Vickylance · Jul 08, 2015 at 09:16 AM · gettouch

A problem with GetTouch(0) and GetTouch(1)

Guys I have created a TouchJoystick.cs but I have a problem in it. When I tap and hold on left side of the screen it says GetTouch(0) is in left side...ok so far so good, but when I simultaneously tap and hold on the right side of the screen it only sometimes says that GetTouch(1) is in the right side (which is what I want)... but sometimes It says GetTouch(0) is in the right side (or in other words though I was still holding down GetTouch(0) in the left side somehow it transfered it to the right side and made left side GetTouch(1) ) ... please help me.. here's the code.

http://pastebin.com/Rp8RqAfQ

And here are the two images needed for the script,

Joypad Base

and

Joypad Stick

joypadbase1.png (3.9 kB)
joypadstick1.png (614 B)
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

2 Replies

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

Answer by Bunny83 · Jul 09, 2015 at 01:03 AM

Here's an example:

 public GameObject joypadBase1;
 public GameObject joypadStick1;
 public GameObject joypadBase2;
 public GameObject joypadStick2;
  
 public Camera camera;
 
 private int joypadFingerID1 = -1;
 private int joypadFingerID2 = -1;
 
 void Update()
 {
     for (int i = 0; i < Input.touchCount; i++)
     {
         var t = Input.GetTouch(i);
         var pos = t.position;
         pos.z = 1.0f;
         pos = camera.ScreenToWorldPoint(pos);
         if (t.phase == TouchPhase.Began)
         {
             if (t.position.x < Screen.width*0.5f)
             {
                 joypadFingerID1 = t.fingerId;
                 joypadBase1.transform.position = pos;
                 joypadStick1.transform.localPosition = Vector3.zero;
             }
             else
             {
                 joypadFingerID2 = t.fingerId;
                 joypadBase2.transform.position = pos;
                 joypadStick2.transform.localPosition = Vector3.zero;
             }
         }
         else if(t.phase == TouchPhase.Moved)
         {
             if (t.fingerId == joypadFingerID1 )
                 joypadStick1.transform.position = pos;
             if (t.fingerId == joypadFingerID2 )
                 joypadStick2.transform.position = pos;
         }
         else if(t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled)
         {
             if (t.fingerId == joypadFingerID1 )
                 joypadFingerID1 = -1;
             if (t.fingerId == joypadFingerID2 )
                 joypadFingerID2 = -1;
         }
     }
     // This usually isn't necessary but is always good for safety
     if (Input.touchCount == 0)
     {
         joypadFingerID1 = -1;
         joypadFingerID2 = -1;
     }
 }

Note: This currently is ment to only work for one finger on each side. If you have more than two fingers on one side, always the last one counts. If you want it to keep the first you have to check "joypadFingerID 1 / 2" for being "-1" when the "Began" event happens. So you only assignn the fingerID when it doesn't have one yet.

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 Vickylance · Jul 09, 2015 at 08:27 AM 0
Share

@Bunny83 Wow thanks a lot... :) You have really saved my day. It worked just like I wanted and I hope to use this technique whenever I wanted to implement touch features :) I have just modified it a bit to suit my needs and here is the code :) using UnityEngine; using System.Collections;

 public class TouchPlay5 : $$anonymous$$onoBehaviour {
 
     public GameObject joypadBase1;
     public GameObject joypadBase2;
     public GameObject joypadStick1;
     public GameObject joypadStick2;
 
     public Camera camera;
 
     private int joypadFingerID1 = -1;
     private int joypadFingerID2 = -1;
 
     void Update () 
     {
         for (int i = 0; i < Input.touchCount; i++) 
         {
             Touch t = Input.GetTouch (i);
             Vector3 pos = t.position;
             pos.z = 1.0f;
             pos = camera.ScreenToWorldPoint(pos);
             if(t.phase == TouchPhase.Began)
             {
                 if(t.position.x < Screen.width/2)
                 {
                     if(joypadFingerID1 == -1)
                     {
                         Debug.Log("left side, ID: " + t.fingerId + " ,began.");
                         joypadFingerID1 = t.fingerId;
                         joypadBase1.transform.position = pos;
                         joypadStick1.transform.localPosition = Vector3.zero;
                     }
                 }
                 else
                 {
                     if(joypadFingerID2 == -1)
                     {
                         Debug.Log("right side, ID: " + t.fingerId + " ,began.");
                         joypadFingerID2 = t.fingerId;
                         joypadBase2.transform.position = pos;
                         joypadStick2.transform.localPosition = Vector3.zero;
                     }
                 }
             }
             else if(t.phase == TouchPhase.$$anonymous$$oved)
             {
                 if(t.fingerId == joypadFingerID1)
                 {
                     joypadStick1.transform.position = pos;
                 }
                 if(t.fingerId == joypadFingerID2)
                 {
                     joypadStick2.transform.position = pos;
                 }
             }
             else if(t.phase == TouchPhase.Ended)
             {
                 if(t.fingerId == joypadFingerID1)
                 {
                     joypadFingerID1 = -1;
                     joypadStick1.transform.localPosition = Vector3.zero;
                 }
                 if(t.fingerId == joypadFingerID2)
                 {
                     joypadFingerID2 = -1;
                     joypadStick2.transform.localPosition = Vector3.zero;
                 }
             }
         }
         if(Input.touchCount == 0)
         {
             joypadFingerID1 = -1;
             joypadFingerID2 = -1;
         }
         Debug.Log ("joystick 1 val: " + JoystickValues(joypadStick1.transform.localPosition) + " joystick 2 val: " + JoystickValues(joypadStick2.transform.localPosition));
     }
 
     public Vector2 JoystickValues(Vector3 joystickPosition)
     {
         Vector2 joystickPosReq = new Vector2(joystickPosition.x,joystickPosition.y);
         return joystickPosReq;
     }
 }
 

Again thanks for your kind help:)

avatar image
2

Answer by Graham-Dunnett · Jul 08, 2015 at 09:18 AM

Please read the documentation:

http://docs.unity3d.com/ScriptReference/Touch-fingerId.html

You'll see that the order of touches is guaranteed to change, which is why there is an id to make you easily track touches over time.

Comment
Add comment · Show 5 · 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 Vickylance · Jul 08, 2015 at 02:31 PM 0
Share

is it 100% Guarantee that the finger ID wont change? And also does it mean that I should never use the GetTouch Function?

avatar image Bunny83 · Jul 08, 2015 at 02:47 PM 0
Share

@Vickylance: It is gueranteed that the fingerID doesn't change for a "tracked finger". A finger is tracked once the touch phase "Began" occured and the tracking ends once you get the phase "Cancelled" or "Ended".

Your problem is most likely that the left finger has interruptions. This can easily happen if you have your two touches on the same line (horizontally or vertically). If that happens the tracking of the left finger ended for a short time. If now the right finger just "starts" touching (so you get a Began) it will get the first finger ID since no touch was active at that moment.

You can use my $$anonymous$$obileStats app if you have an android device. If you're on iOS, i'm sorry, i don't have an iOS build ^^. But here's the one and only script that makes up the whole app. You're just missing the used GUISkin but basically it's jsut an empty scene with that script attached to the camera.

With that app you can see the index of the touch as well as the fingerID

edit
oops, I just realised that i actually use another script for drawing my bezier lines ^^. Here it is. That one isn't cleaned up. It just contains some utility methods.

avatar image Vickylance · Jul 08, 2015 at 03:02 PM 0
Share

@Bunny83 oh ok will try that app, andlet you know.... just one doubt you said,

Your problem is most likely that the left finger has interruptions. This can easily happen if you have your two touches on the same line (horizontally or vertically). If that happens the tracking of the left finger ended for a short time. If now the right finger just "starts" touching (so you get a Began) it will get the first finger ID since no touch was active at that moment.

Does it mean that the FingerID can even change if this problem occurs? And you also said,

It is gueranteed that the fingerID doesn't change for a "tracked finger". A finger is tracked once the touch phase "Began" occured and the tracking ends once you get the phase "Cancelled" or "Ended".

Is this not the case with Gettouch Function? Anyways now I am trying to convert all the Gettouch functions into fingerID functions which is a bit tough, will let you know if it all works.

avatar image Bunny83 · Jul 08, 2015 at 03:10 PM 0
Share

@Vickylance:
As soon as a finger is no longer recognised by the touch hardware it is no longer tracked. If it "reappear" again the hardware has no relation to that finger anymore. It's a new finger to the system. As i said as long as you don't get a Cancelled or Ended for a certain fingerID it will always be the same ID.

GetTouch just takes an index to get "a touch" from the currently available ones.

Usually you use a for loop to loop through all touches and do your stuff there. For each logical control the user can control you should declare an integer variable that hold "-1" if it's not being used or the fingerID that is currently associated with your control. You assign the fingerID in "Began" and clear it in "Cancelled / Ended". It's actually pretty easy once you understand how the hardware works.

avatar image Vickylance · Jul 08, 2015 at 05:47 PM 0
Share

@Bunny83 Ok I get the difference between them but sorry to bother you again but could you please explain what you said here better

Usually you use a for loop to loop through all touches and do your stuff there. For each logical control the user can control you should declare an integer variable that hold "-1" if it's not being used or the fingerID that is currently associated with your control. You assign the fingerID in "Began" and clear it in "Cancelled / Ended". It's actually pretty easy once you understand how the hardware works.

I couldn't get it.. Again sorry to bother you again :(.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Strange touch behavior? 2 Answers

using Input.GetTouch(0).deltaPosition to move camera 1 Answer

Transitioning from different number of touches causes behavior overlap 0 Answers

touch and destroy clone object with specific color 0 Answers

help with touch versus keyboard controls 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