Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Matthewek · Sep 04, 2015 at 04:30 PM · uitouchbuttonsmultitouch

Touch area binding

I have created a button in my 2d game using Unity3d, added box collider 2d with name PADBASE to detect touch events this way:

 if(Input.touchCount > 0)
 {
     for(int i = 0; i < Input.touchCount; i++)
     {
         Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
         Vector2 mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
         Vector2 dir = Vector2.zero;
         RaycastHit2D hit = Physics2D.Raycast(mousePos2D, dir);
         Touch t = Input.GetTouch(i);
         
         if (hit.transform != null)
         {
             if(Physics2D.Raycast (hit.transform.position , hit.transform.forward))
             {
                 GameObject recipient = hit.transform.gameObject;
                 
                 if(t.phase == TouchPhase.Began) //poczatek dotyku
                 {
                     // button clicked
 
                     // change colour to red to visually show that button is being pressed
                 }
                 else if (t.phase == TouchPhase.Ended)
                 {
                     // change collor to its default colour to visually show that its no longer pressed
                 }
             }
 
         }
     }
 }

And lets assume I will change button colour to red when player touched the button, and back to its default colour when he released his finger (for example)

Now it will obviously work only when player will release his finger, as long as his finger is actually inside bounds of the box collider, what I am trying to do is "bind touch events(?)" to still catch touch event (slide or ended) even if player moved his finger outside of the collider without releasing his finger (for example accidentally)

I am looking forward for some suggestions, thanks.

In my game I will have multiple buttons so multi touch is necessary.

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 Matthewek · Sep 07, 2015 at 12:08 PM

Solved, it actually turns to be really easy, you need to store and compare touch finger ID.

Solution:

- on touch began:

check if touch is withing game objects box collider like in the code attached above.

get touch finger ID and pass it to your button or keep reference of it

- on touch ended:

compare touch finger ID with your stored touch id, if they are equal, set your stored finger id to -1 and perform your code that should execute on touch edned

- on touch moved or (||) stationary:

do the same comparison of the touch id like in the touch ended, and perform your code that should be executed if player will slide/move his finger, but do not modify your finger id variable.

That`s it, works well with multiple buttons with multi touch.

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 sourav13 · Jun 24, 2016 at 07:28 PM 0
Share

@$$anonymous$$atthewek , i'm not able to get the point storing the touch.FingerId in variable , no matter where you touch you will still have fingerId. and how would this comparison stand , little hard to understand ,can you show code example Thank you

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

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

Related Questions

find an especific multitouch wich is on a image in the canvas 0 Answers

Why is my multi touch not working? 1 Answer

UI buttons sometimes does not detect touch 1 Answer

How to hide x number of button while the button its doing something.. 0 Answers

multiple buttons to open world canvases in vr 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