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 /
avatar image
0
Question by Daniel G · Aug 04, 2013 at 08:02 PM · javascriptiphonemultitouchunderstanding logic

Multitouch iPhone error! Help

Hello,

I'm running into a simple error with Multitouch.

First:

Line referenced = var touch1 = Input.GetTouch(0);

alt text

 function Update () {
 
 //iPhone Touch code
             var tapCount = Input.touchCount;
             if(tapCount > 0) {
                 print(tapCount);
                        var touch1 = Input.GetTouch(0);
                          var touch2 = Input.GetTouch(1);
 
 //---------------------------------Touch1--------------------------------------------------               
                    if (touch1.phase == TouchPhase.Began){ // if screen touch...
                       // create a ray passing through the touch pos:
                      var iphoneray1: Ray = Camera.main.ScreenPointToRay(touch1.position);
                      var iphonehit1:RaycastHit;
                         if (Physics.Raycast(iphoneray1, iphonehit1)){ // do a raycast
                     var ObjHit1 = iphonehit1.collider.gameObject; 
                     Vehicle.BroadcastMessage("iPhoneInputTap1", ObjHit1.name);
                     Debug.Log (ObjHit1.name + "Touch1");
                          }
                      } else if (touch1.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Canceled) {
                       Vehicle.BroadcastMessage("iPhoneInputTap1", "TapEnded1");
                      }
 //---------------------------------Touch2--------------------------------------------------      
                 if (touch2.phase == TouchPhase.Began){ // if screen touch...
                       // create a ray passing through the touch pos:
                      var iphoneray2: Ray = Camera.main.ScreenPointToRay(touch2.position);
                      var iphonehit2:RaycastHit;
                         if (Physics.Raycast(iphoneray2, iphonehit2)){ // do a raycast
                     var ObjHit2 = iphonehit2.collider.gameObject; 
                     Vehicle.BroadcastMessage("iPhoneInputTap2", ObjHit2.name);
                     Debug.Log (ObjHit2.name + "Touch2");
                          }
                      } else if (touch2.phase == TouchPhase.Ended || touch2.phase == TouchPhase.Canceled) {
                       Vehicle.BroadcastMessage("iPhoneInputTap2", "TapEnded2");
                      }
 
 }
 


Thanks Daniel

screen shot 2013-08-04 at 2.54.18 pm.png (8.4 kB)
screen shot 2013-08-04 at 4.03.53 pm.png (8.3 kB)
Comment
Add comment · Show 6
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 Graham-Dunnett ♦♦ · Aug 04, 2013 at 08:09 PM 1
Share

You're checking if TouchCount is greater than 0. If it's 1, then your code access touch0 and touch1. You should only access touch1 is TouchCount is 2 or higher.

avatar image Lovrenc · Aug 04, 2013 at 08:12 PM 0
Share

I never programmed any touchscreen stuff but i guess problem is here:

 var touch2 = Input.GetTouch(1);

check if you actually have multiple touches.

avatar image Daniel G · Aug 04, 2013 at 08:20 PM 0
Share

@Lovrenc Yes "tapCount" when printed registers 1 or 2 or 3 touches based on the amount of fingers touching screen.

avatar image Daniel G · Aug 04, 2013 at 08:26 PM 0
Share

@GrahamDunnett That makes me have to put in TWO fingers for it to register a touch! What are you trying to say?

avatar image Lovrenc · Aug 04, 2013 at 09:16 PM 1
Share

Listen to your sentence.

Yes "tapCount" when printed registers 1 or 2 or 3

But in this sentence you say:

 var touch2 = Input.GetTouch(1); //Yo Yo unity, give me the second touch!!!! GIVE IT TO $$anonymous$$E!


But sometimes, when you only have 1 touch, poor unity cannot give you the second touch. He wants to but he cant. So he pees himself (throws this exception).

Show more comments

1 Reply

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

Answer by Lovrenc · Aug 04, 2013 at 08:59 PM

 function Update () {
  
 //iPhone Touch code
          var tapCount = Input.touchCount;
          if(tapCount > 0) {
           print(tapCount);
                    var touch1 = Input.GetTouch(0);
  
 //---------------------------------Touch1--------------------------------------------------          
              if (touch1.phase == TouchPhase.Began){ // if screen touch...
                  // create a ray passing through the touch pos:
                var iphoneray1: Ray = Camera.main.ScreenPointToRay(touch1.position);
                var iphonehit1:RaycastHit;
                   if (Physics.Raycast(iphoneray1, iphonehit1)){ // do a raycast
               var ObjHit1 = iphonehit1.collider.gameObject; 
               Vehicle.BroadcastMessage("iPhoneInputTap1", ObjHit1.name);
               Debug.Log (ObjHit1.name + "Touch1");
                     }
                 } else if (touch1.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Canceled) {
               Vehicle.BroadcastMessage("iPhoneInputTap1", "TapEnded1");
                 }
 //---------------------------------Touch2--------------------------------------------------     
           if (tapCount > 1 && touch2.phase == TouchPhase.Began){ // if screen touch...
                  // create a ray passing through the touch pos:
                var touch2 = Input.GetTouch(1);
                var iphoneray2: Ray = Camera.main.ScreenPointToRay(touch2.position);
                var iphonehit2:RaycastHit;
                   if (Physics.Raycast(iphoneray2, iphonehit2)){ // do a raycast
               var ObjHit2 = iphonehit2.collider.gameObject; 
               Vehicle.BroadcastMessage("iPhoneInputTap2", ObjHit2.name);
               Debug.Log (ObjHit2.name + "Touch2");
                     }
                 } else if (touch2.phase == TouchPhase.Ended || touch2.phase == TouchPhase.Canceled) {
               Vehicle.BroadcastMessage("iPhoneInputTap2", "TapEnded2");
                 }
  
 }
Comment
Add comment · Show 2 · 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 Daniel G · Aug 04, 2013 at 09:31 PM 0
Share

Okay! Yes! I noticed that and made this :P will this do the same thing?

 //iPhone Touch code
             var tapCount = Input.touchCount;
         if (tapCount > 1) {
             switch (tapCount) {
                 case 1 :  
                     var touch1 = Input.GetTouch(0); 
                     print(tapCount);
                 break;
                 
                 case 2 :
                     touch1 = Input.GetTouch(0);
                      var touch2 = Input.GetTouch(1);
                      print(tapCount);
                  break;
                  
                  case 3 :
                       touch1 = Input.GetTouch(0);
                      touch2 = Input.GetTouch(1);
                      var touch3 = Input.GetTouch(2);
                      print(tapCount);
                  break;
                  
                  case 4 :
                      touch1 = Input.GetTouch(0);
                      touch2 = Input.GetTouch(1);
                      touch3 = Input.GetTouch(2);
                      var touch4 = Input.GetTouch(3);
                      print(tapCount);
                  break;
                  
                  case 5 :
                      touch1 = Input.GetTouch(0);
                      touch2 = Input.GetTouch(1);
                      touch3 = Input.GetTouch(2);
                      touch4 = Input.GetTouch(3);
                      var touch5 = Input.GetTouch(4);
                      print(tapCount);
                  break;
             }
         }

@Lovrenc

avatar image Daniel G · Aug 04, 2013 at 09:55 PM 0
Share

@Lovrenc I just realized that if i did it this way i would have to put both fingers down at the same time, this would NOT work your example will! ThankYou!

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

15 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

Related Questions

Multiple Cars not working 1 Answer

TPS Camera with Joystick (Android) 0 Answers

"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 1 Answer

Every 0.5 sec there is a 10% chance of a target spawning. 2 Answers

3d text will not display float 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