- Home /
 
Multi touch for character movement on android[solved]
Ok guys , I have been trying to solve this problem for quite some time now. This version of the code is just for testing , I know it will only work if i follow the pattern my code describes. I have tried with a for loop and GetTouch(i) and when it didn't work I thought to try it like this, and the same problem persists. The problem i'm having is that when I am moving with my character, and then i tap to rotate him, it seems that he isn't moving that much (or not at all). I have 3 GUITextures , one for moving the character in the forward direction, and 2 for turning left or right. Currently I am testing with only the move and turn left buttons. Here is the snippet:
EDIT: New code for handling touch
 // Loop through all the touches on screen if there are any
     if(Input.touchCount > 0){
         for(int i=0;i<Input.touchCount;i++){
             Touch t = Input.GetTouch(i);
             // Check to see if touch is on move_forward button
             if(move_forward.HitTest(t.position) && t.phase != TouchPhase.Ended && t.phase != TouchPhase.Canceled){
                 doMove=true;
             }
             // Check to see if touch is on rotate left button
             if(turn_left.HitTest(t.position) && t.phase != TouchPhase.Ended && t.phase != TouchPhase.Canceled){
                 doTurnLeft=true;
             }
             // Check to see if touch is on rotate right button
             if(turn_right.HitTest(t.position) && t.phase != TouchPhase.Ended && t.phase != TouchPhase.Canceled){
                 doTurnRight=true;
             }
         }
     }
 
               New code for handling movement:
 if (doMove == true && (doTurnLeft == true || doTurnRight == true) )  {
             // Translate to new position
             Vector3 position = transform.position;
             position+=transform.forward*move_speed;
             transform.position=position;
 
             // Rotate
             if(doTurnRight == true)
                 transform.Rotate(transform.up,rotation_speed);
             else
                 transform.Rotate(transform.up,rotation_speed*-1.0f);
 
             // Play walk animation
             animation.Play("walk");
         }
         if (doMove == true && doTurnLeft == false && doTurnRight == false) {
             // Translate to new position
             Vector3 position = transform.position;
             position+=transform.forward*move_speed;
             transform.position=position;;
         
             // Play walk animation
             animation.Play ("walk");
         }
         if (doMove == false && (doTurnLeft == true || doTurnRight == true) ) {
             // Rotate
             if(doTurnRight == true)
                 transform.Rotate(Vector3.up*rotation_speed);
             else
                 transform.Rotate(Vector3.up*rotation_speed*-1.0f);
 
             // Play idle animation
             animation.Play ("idle");
         }
         if (doMove == false && doTurnLeft == false && doTurnRight == false) {
             //Play idle animation
             animation.Play("idle");
         }
 
               At the start of the Update function I reset the values for doMove,etc to false, that's why I'm only making them true with my conditions.
LE: So the multi touch seems to work but when i have two touches on screen (one touch on move and one touch simply anywhere else , not on any other button) my character's speed slows down ,and I can't figure out what the problem is.
LE2: Notice how in the above code the character transformations occur outside the for loop. If i move them inside the foor loop then the problem disappears. Why is that ?
New code 2(working) :
 / Loop through all the touches on screen if there are any
         if (Input.touchCount > 0)
         {
             for (int i = 0; i < Input.touchCount; i++)
             {
 
                 Touch t = Input.GetTouch(i);
                 switch (t.phase)
                 {
                     case TouchPhase.Began:
                         if (move_forward.HitTest(t.position))
                         {
                             moveFingerID = t.fingerId;
                             doMove = true;
                         }
                         else if (turn_left.HitTest(t.position))
                         {
                             turnLFingerID = t.fingerId;
                             doTurnLeft = true;
                         }
                         else if (turn_right.HitTest(t.position))
                         {
                             turnRFingerID = t.fingerId;
                             doTurnRight = true;
                         }
                         break;
                     case TouchPhase.Stationary:
                         if (doMove && moveFingerID == t.fingerId)
                         {
                             Vector3 position = transform.position;
                             position += transform.forward * move_speed * 1.0f * 1.0f;
                             transform.position = position;
 
                            
                         }
                        
                         if (doTurnLeft && turnLFingerID == t.fingerId)
                         {
                             transform.Rotate(Vector3.up * rotation_speed * -1.0f * 1.0f);
                         }
                         if (doTurnRight && turnRFingerID == t.fingerId)
                         {
                             transform.Rotate(Vector3.up * rotation_speed * 1.0f);
                         }
                         break;
                     case TouchPhase.Ended:
                         if (moveFingerID == t.fingerId)
                         {
                             moveFingerID = -1;
                             doMove = false;
 
                         }
                         else if (turnLFingerID == t.fingerId)
                         {
                             turnLFingerID = -1;
                             doTurnLeft = false;
                         }
                         else if (turnRFingerID == t.fingerId)
                         {
                             turnRFingerID = -1;
                             doTurnRight = false;
                         }
                         break;
                     default: break;
 
                 }
             }
             if(doMove)
                 animation.Play("walk");
             else
                 animation.Play("idle");
         }
         else
             animation.Play("idle");
 
              A new update with my problem, if i am holding the move button and then i touch the screen(not on the other buttons-> so no rotate action) my character's speed slows down. What is causing this?
Answer by robertbu · May 06, 2014 at 07:53 PM
Multi-touch is complicated. You cannot depend on the 'GetTouch()' index remaining the same from call to call. Looking at your code you may be able to just test all fingers against both GUITextures. But if you need to track individual fingers, then you need to do it by Touch.fingerId, not by index. But to start with, put in a foreach() loop and test all touches against the two GUITextures, setting 'doTurnLeft' and 'doMove'. Then process the 'doMove' and 'doTurnLeft' after the loop.
Yes I am well aware that their index changes during the game but with my updated code it makes no difference, it should read them both. $$anonymous$$y problem is that when i touch 2 buttons or even my move button and another touch on screen my character translates slower, thats why when i'm rotating he seems to stand still. Check my edit
I managed to locate the problem and I will soon mark the question as solved but I would like to know if someone has a good explanation for why this is problematic. Please check the edited question for the solution.
For all who face this problem, and the code seems correct. $$anonymous$$ake sure you don't have a mouse event in it as i believe it gets triggered by the touch also. Remove all mouse handlers when making the final build for your game on mobile devices.
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Problem with multi touch[solved] 0 Answers
Changing the size of a guiTexture as a health bar (using C#) 1 Answer
Multi touches problem 0 Answers