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 Diekeke · Jun 05, 2013 at 02:27 PM · ioscolliderinputbuttontouch

Help with touch input for iOS please!

Well, I have been working in a system for touch input for my project for weeks now. I've tried with several different codes but none of them work properly. Here are some of them:

 var i : int;
 var hit : RaycastHit;

  function Update () 
     {
         for(i = 0; i< Input.touchCount; i++)
             {    
                 var ray : Ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                 if(Physics.Raycast(ray, hit))
                 {
                     if(collider.bounds.Contains(hit.transform.localPosition))
                     {
                         if(Input.GetTouch(i).phase == TouchPhase.Began)
                         {    
                             hit.transform.gameObject.SendMessage("OnTouchDown");
                         }
                         if(Input.GetTouch(i).phase == TouchPhase.Ended)
                         {    
                             hit.transform.gameObject.SendMessage("OnTouchUp");
                         }
                     }
                 }
             }
     }

The problem with this one is that it doesn't do anything when tapping objects that are inside other objects (children objects). Aside from that it works good.

Next:

 var i : int;
 var hit : RaycastHit;

 function Update () 
     {
         for(i = 0; i< Input.touchCount; i++)
             {
                 if(Input.GetTouch(i).phase == TouchPhase.Began)
                 {
                    var ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);
                         if(Physics.Raycast(ray, hit))
                         {
                        hit.transform.gameObject.SendMessage("OnTouchDown");
                     }
                  }
                 
                 if(Input.GetTouch(i).phase == TouchPhase.Ended)
                    {
                     hit.transform.gameObject.SendMessage("OnTouchUp");
                    }
             }
     }

This code also works, but I think that for some reason it shoots a ton of rays on a single touch, the reason I believe this is because when I tap a button on the Main Menu of my game, it automatically taps the button that appears after selecting the first one. Another reason is that while playing, in theory if you tap a block it gives you one point and then the block disappears. When using this code, if you tap a block it gives you 9 points before disappearing.

Note: I am using this code as the "brain" for all the codes of the buttons (`class ScriptName extends TouchButtonScript`). I am using a 3DText with a box collider as my buttons.

Does anybody knows what I'm doing wrong? Thanks in advance.

Comment
Add comment
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

1 Reply

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

Answer by Coderdood · Jun 05, 2013 at 06:47 PM

Problem 1:

The problem with this one is that it doesn't do anything when tapping objects that are inside other objects (children objects).

GameObject.SendMessage = "Calls the method named methodName on every MonoBehaviour in this game object."

Use GameObject.BroadCastMessage = "Calls the method named methodName on every MonoBehaviour in this game object or any of its children."

Note that you will get an error message if there was no receiver found unless you use SendMessage.DontRequireReceiver. I've missed that bit of documentation in the past.

I suggest you use the second script instead anyway.

Problem 2:

This code also works, but I think that for some reason it shoots a ton of rays on a single touch, the reason I believe this is because when I tap a button on the Main Menu of my game, it automatically taps the button that appears after selecting the first one.

The problem is this script should be attached to a only one object in the scene. By using this as the base (class ScriptName extends TouchButtonScript) every time you click a raycast for every object extending TouchButtonScript is send.

Create a single "TouchManager" or some such object and attach this script to that. I actually recommend this version over the first.

[4]: http://answers.unity3d.com/questions/202037/touchphase-firing-too-much-ios.html

Comment
Add comment · Show 1 · 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 Diekeke · Jun 06, 2013 at 01:46 AM 0
Share

Thank you, I used the second solution (for the second script). I added the TouchButtonScript to the $$anonymous$$ain Camera and erased the (class ... .. .. ) from all the scripts, it works great!

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

Detect a tap versus standing touch on iPhone? 3 Answers

Touch input reporting 0 delta for first several Updates on iOS, 2 Answers

How to replace input axis keyboard button with touch button? 3 Answers

Drag and Drop button on Mobile (Messenger style) 0 Answers

UI button touch input problem when UI is SetActive during game play 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