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 /
avatar image
0
Question by king47 · Jul 15, 2015 at 09:34 PM · loopmultitouchfor loopfor

Android multitouch problem

I've recently come across a problem regarding implemanting multitouch. I've looked at unity's own multitouch help but even when i'm copying the code and try it, it doesn't work.

This thread shows how to do it: http://answers.unity3d.com/questions/167750/how-does-multi-touch-work.html

  //The good way;
  var tapCount = Input.touchCount;
  for ( var i = 0 ; i < tapCount ; i++ ) {
        var touch = Input.GetTouch(i);
 
 
 if( gasButton.HitTest (Input.GetTouch(i).position)) 
      {
           throttleForward = 1;
      }else 
      {
        throttleForward = 0;
      }
 
 if( rightButton.HitTest (Input.GetTouch(i).position)) 
      {
       stearC = 1;
      }else 
      {
           stearC = 0;
      }
 
 }


Now I copied the code and I am not able to press the gasbutton and rightbutton at the same time. I can only press one button at the same time. Isn't the for loop changing the i value between 1 - 2 and jumping between the touch positions and run the if statements? I've must have gotten it all wrong.

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 king47 · Jul 15, 2015 at 09:46 PM 0
Share

Never the less this code should work but the lines i have put into it is not right. I would be grateful if somebody could correct my mistakes

1 Reply

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

Answer by maccabbe · Jul 15, 2015 at 09:57 PM

I think you're right that the problem is that both values are changing for both touches. Right now what would occur if you tried to press both buttons is

touch 1 presses gassButton but not rightButton so it sets throttleForward to 1 and stearC to 0

touch 2 presses gassButton but not rightButton so it sets throttleForward to 0 and stearC to 1

so while both touch 1 and touch 2 are being processed it is done in such a way that touch 2 is overriding all effects of touch 1. Instead it would make sense to have the buttons in their default state of 0 and set to 1 only if a touch presses them. For instance

 throttleForward = 0;
 stearC = 0;
 var tapCount = Input.touchCount;
 for ( var i = 0 ; i < tapCount ; i++ ) {
     var touch = Input.GetTouch(i);    
     
     if( gasButton.HitTest (Input.GetTouch(i).position)) 
     {
         throttleForward = 1;
     }    
     if( rightButton.HitTest (Input.GetTouch(i).position)) 
     {
         stearC = 1;
     }    
 }

Now what should happen is

throttleForward and stearC are both set to 0

touch 1 presses gassButton but not rightButton so it sets throttleForward to 1

touch 2 presses rightButton but not gassButton so it sets stearC to 1

now both throttleForward and stearC are 1

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 king47 · Jul 15, 2015 at 10:35 PM 0
Share

It doesn't help. These controls will be used for controlling vehicles. So i want to be able to throttle and turn at the same time. Now i notice as you said before that the second touch overrides the first one. But isn't the for loop supposed to change the i value (1-2) and jump from finger to finger every update and check the current finger's position and see if that finger's position is on one of the buttons? It would do it so fast that you would not notice that it actually only handles one finger at a time.

avatar image maccabbe · Jul 15, 2015 at 10:57 PM 0
Share

I don't see how this doesn't help. As I explained, the code allows you to activate both buttons at once using two different touches.

The for loop is used to go through all touches every update which is what you should be doing. It is possible to change it so it cycles through the touches and processes only one touch every update, but what then?

Best case scenario is that ins$$anonymous$$d of your input overriding each other every update you have your touches overriding each other every 2 updates. It'll cause other problems that you'll have to address in addition to the original problem which doesn't seem like it would actually be solved.

avatar image king47 · Jul 15, 2015 at 11:16 PM 0
Share

Oh! never $$anonymous$$d, I done goofed :) Forgot to change the code before building it. Thanks a lot :D

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

2 People are following this question.

avatar image avatar image

Related Questions

For loop is being totally ignored 1 Answer

"for" loop - DrawTexture problem. 0 Answers

Shorten code with FOR loop 1 Answer

For loop C#? 2 Answers

Using for as while. 3 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