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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by DRazvan · May 06, 2014 at 06:13 PM · c#guitexturemultitouchcharacter.move

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");
Comment
Add comment · Show 1
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 DRazvan · May 06, 2014 at 07:23 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · 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 DRazvan · May 07, 2014 at 09:04 AM 0
Share

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

avatar image DRazvan · May 24, 2014 at 01:48 PM 0
Share

Bump, still having the problem, anyone has any ideas?

avatar image DRazvan · May 24, 2014 at 07:57 PM 0
Share

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.

avatar image DRazvan · Jun 27, 2014 at 10:50 AM 0
Share

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

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

20 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 avatar image avatar image avatar image avatar image

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


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