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 dadika4 · Aug 17, 2015 at 10:51 AM · androidmultitouchfingerid

Multitouch Android touches excluding each other

When i press one button lets say left iworks but when i press jump the jump starts but left movement stops.....

 using UnityEngine;
 using System.Collections;
 
 public class touchControls : MonoBehaviour {
 
     public GameObject player;
     public bool movingleft = false;
     public bool movingright = false;
     public bool grounded;
     public float jumpForce = 100;
     public int finger1 = 1;
     public int finger2 = 2;
     public int finger3 = 3;
     public int finger4 = 4;
     public Touch touch;
     Rigidbody2D rig;
 
     void Start () {
         rig = GameObject.FindGameObjectWithTag ("player").GetComponent<Rigidbody2D> ();
            
     }
     
 
     void Update () {
 
           grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;    
         
         if (grounded) {
             //sr.sprite = gor;
         } else if (!grounded) {
         //    sr.sprite = dol;
         }
     
         if (Application.platform == RuntimePlatform.Android) {
             if (Input.touchCount > 0) {
                 for (int i=0; i<Input.touchCount; i++) {
                     if(Input.GetTouch (i).phase == TouchPhase.Began){
                         CheckTouch(Input.GetTouch(i).position, "begin");
                     }
                     else if(Input.GetTouch (i).phase == TouchPhase.Ended){
                         if(touch.fingerId == finger1){
                             finger1 = 1;
                             movingleft = false;
                         }
                         if(touch.fingerId == finger2){
                             finger2 = 2; 
                             movingright = false;
                         }
                         if(touch.fingerId == finger3){
                             finger3 = 3;
                         }
                     }
                 }
             }
         }
 
         if (movingleft) {
             player.transform.Translate (Vector2.left * Time.deltaTime * 5f);
         }
         if (movingright) {
             player.transform.Translate (Vector2.right * Time.deltaTime * 5f);
         }
 
     }
 
     void CheckTouch(Vector3 pos, string phase){
 
         Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
         Vector2 touchPos = new Vector2(wp.x, wp.y);
         Collider2D hit = Physics2D.OverlapPoint(touchPos);
         
         if (hit.gameObject.name == "levogor" && hit && phase == "begin"){
             movingleft = true;
             finger1 = touch.fingerId;
         }
         if (hit.gameObject.name == "desnogor" && hit && phase == "begin"){
             movingright = true;
             finger2 = touch.fingerId;
         }
         if (hit.gameObject.name == "jump" && hit && phase == "begin" && grounded) {
             rig.AddForce (new Vector2 (0f, jumpForce));
             finger3 = touch.fingerId;
         }    
     
     } 
 
 }





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 dadika4 · Aug 18, 2015 at 09:10 PM 0
Share

Nope that didnt worked....i will go with other approach with manipulating foreach(Touch t in Input.touches)......

I think there is a problem when you use incrementig touches because update is rearanging array all the time. But for each i think it looks the current content of the array.... There is no sigle right way people are using this have looked all the related prblems here on unity and stark overflow and there is something fishy about this problem... it seems like nobody came to the bottom of it..... BUT I Will....

avatar image dadika4 · Aug 18, 2015 at 09:10 PM 0
Share

Now this is funny.... I have deleted the approach with fingerID and i get the same results. When i press the left button i go left when i stop pressing it stops. When i press left button and press somewhere on the screen the left button is interupted and the gameobject stops. if i press left or right i go in the direction i press jump i jump but the movement gest interupted. I think there is a problem because i use outside method and then the update is interupted. i will try to do every thing in update this i think will be the solution.

NeverHopeless thank you for your help. If you have some other hint dont hesitate. The result of the code below is described in the begining. I will post new one a bit later when i try the solution.

 using UnityEngine;
 using System.Collections;
 
 public class touchControls : $$anonymous$$onoBehaviour {
     
     public GameObject player;
     public bool movingleft = false;
     public bool movingright = false;
     public bool grounded;
     public float jumpForce = 100;
     public int finger1 = 1;
     public int finger2 = 2;
     public int finger3 = 3;
     public Touch touch;
     Rigidbody2D rig;
     
     void Start () {
         rig = GameObject.FindGameObjectWithTag ("player").GetComponent<Rigidbody2D> ();
         
     }
     
     
     void Update () {
         grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;    
         
         if (grounded) {
             //sr.sprite = gor;
         } else if (!grounded) {
             //    sr.sprite = dol;
         }
         
         if (Application.platform == RuntimePlatform.Android) {
             if (Input.touchCount > 0) {
                 for (int i=0; i<Input.touchCount; i++) {
                     if(Input.GetTouch (i).phase == TouchPhase.Began || Input.GetTouch (i).phase == TouchPhase.$$anonymous$$oved){
                         CheckTouch(Input.GetTouch(i).position, "begin");
 
                     }
                      if(Input.GetTouch (i).phase == TouchPhase.Ended){
                         movingleft = movingright = false;
                     
                     }
                 }
             }
         }
         
         if (movingleft) {
             player.transform.Translate (Vector2.left * Time.deltaTime * 5f);
         }
         if (movingright) {
             player.transform.Translate (Vector2.right * Time.deltaTime * 5f);
         }
         
     }
     
     void CheckTouch(Vector3 pos, string phase){
         
         Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
         Vector2 touchPos = new Vector2(wp.x, wp.y);
         Collider2D hit = Physics2D.OverlapPoint(touchPos);
         
         if (hit.gameObject.name == "levogor" && hit && phase == "begin" ){
             movingleft = true;
             //finger1 = touch.fingerId;
 
         }
         if (hit.gameObject.name == "desnogor" && hit && phase == "begin" ){
             movingright = true;
             //finger2 = touch.fingerId;
         }
         if (hit.gameObject.name == "jump" && hit && phase == "begin" && grounded) {
             rig.AddForce (new Vector2 (0f, jumpForce));
         //    finger3 = touch.fingerId;
         }    
         
     } 
     
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NeverHopeless · Aug 17, 2015 at 12:06 PM

Perhaps instead of IF...ELSE... block just using IFs can fix this, because your code only supports one of them due to IF...ELSE....

Try like this:

  void CheckTouch(Vector3 pos, string phase){
 
      Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
      Vector2 touchPos = new Vector2(wp.x, wp.y);
      Collider2D hit = Physics2D.OverlapPoint(touchPos);
      
      if(hit.gameObject.name == "levogor" && hit && phase == "begin"){
          movingleft = true;
          finger1 = touch.fingerId;
      }
      if (hit.gameObject.name == "desnogor" && hit && phase == "begin"){
          movingright = true;
          finger2 = touch.fingerId;
      }
      if (hit.gameObject.name == "jump" && hit && phase == "begin" && grounded) {
          rig.AddForce (new Vector2 (0f, jumpForce));
          finger3 = touch.fingerId;
      }    
  
  } 

Also, finding game object on each update is a performance degradation, like this:

 grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;

Instead capture the reference of game object in the start or from inspector, then try like:

 grounded = playerGO.GetComponent<kontrola> ().ground;



Comment
Add comment · Show 8 · 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 dadika4 · Aug 17, 2015 at 01:05 PM 0
Share

Have set all the else if to ifs and still doesnt work. Grounded must be in update because other script looks if the object is grounded but i will move it to this script about that you are right it is too consumpting....

I will try reconfiguring the touchid logic i think i have missed some of it there.

avatar image NeverHopeless · Aug 17, 2015 at 01:12 PM 0
Share

Where resetting fingerID, set it to -1 ins$$anonymous$$d of 1,2,3.

Like:

                      if(touch.fingerId == finger1){
                          finger1 = -1;
                          movingleft = false;
                      }
                      if(touch.fingerId == finger2){
                          finger2 = -1; 
                          movingright = false;
                      }
                      if(touch.fingerId == finger3){
                          finger3 = -1;
                      }
avatar image dadika4 · Aug 17, 2015 at 07:30 PM 0
Share

Isnt fingerId to store unique identifier for the touches that are stored in Input.touches array. That way i know that my touch was the right one to point it to the right statement.

I have noticed: when i press left i go left and then i press somewhere on the screen.. It interupts my touch and the gameobject stops. I think i have problem in looping the touches array. $$anonymous$$aybe is another way.

I have also written an else statement that returns nothing....

avatar image dadika4 · Aug 17, 2015 at 07:41 PM 0
Share

Still when i make another input touch into an array it interupts other touch.

avatar image NeverHopeless · Aug 17, 2015 at 08:41 PM 0
Share

So you have a script with four variables for four touches each, and with how many games objects this script is attached to ?

Show more comments
avatar image
0

Answer by dadika4 · Aug 31, 2015 at 12:27 PM

I have soved the problem when input.gettouch(i) and touchphase begin or move must be in one loop. Touchphase. end must be in other loop

Than it works

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Drawing 2 lines in same time 0 Answers

FingerID problem 1 Answer

Multitouch sometimes doesn't work properly when fingers are close together. 1 Answer

¿Unity messing up fingerIDs? [Android] 1 Answer

Multitouch physics drag with TargetJoint2D 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