• 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
8
Question by DarkSlash · Jan 13, 2016 at 07:34 PM · 2dinputtouch

Best way to detect touch on a GameObject

I see there're a lot of questions regarding touch detection since ever. As Unity is changing constantly, I want to know what is the best solution to detect touches on a 2D Game Object with the latest version of Unity (5.3).

Im making a Candy Crush clone, and I want to use that touches to move the icons (candies, gems, whatever you want).

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

5 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by rob5300 · Jan 14, 2016 at 11:00 AM

I think the best way to achieve this would be using Raycasting, for this Specifically Physics2D.Raycast().

I don't have access to Unity right now, but from memory you should try something like this:

 using UnityEngine;
 using System.Collections;
 
 public class RaycastTest : MonoBehaviour {
 
     Vector3 touchPosWorld;
 
     //Change me to change the touch phase used.
     TouchPhase touchPhase = TouchPhase.Ended;
 
     void Update() {
         //We check if we have more than one touch happening.
         //We also check if the first touches phase is Ended (that the finger was lifted)
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == touchPhase) {
             //We transform the touch position into word space from screen space and store it.
             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
 
             Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
 
             //We now raycast with this information. If we have hit something we can process it.
             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
 
             if (hitInformation.collider != null) {
                 //We should have hit something with a 2D Physics collider!
                 GameObject touchedObject = hitInformation.transform.gameObject;
                 //touchedObject should be the object someone touched.
                 Debug.Log("Touched " + touchedObject.transform.name);
             }
         }
     }
 }

Again please check this works, as i cannot at the current time of writing. You can change touchPhase to change the TouchPhase used to detect a touch, link to the doc for this Enum is here. Check the documentation for Input.GetTouch() for more examples and information on how to get and use Touches.

You probably want to modify this to check if a touch begins and ends on the same object, but this is a good starting point for basic functionality.

Hope this helps!

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 rob5300 · Jan 14, 2016 at 11:01 AM 0
Share

Note: This can be slightly modified to suit for 3D gameobjects. If there are easier ways to achieve this please comment and correct me.

avatar image DarkSlash · Jan 18, 2016 at 02:35 PM 0
Share

Tried that code and doesn't work :( Nevertheless, I want to know the best way, it doesn't matter if there's code, a link to a tutorial, or just "use raycast and 2d colliders". Right now I can do it using the $$anonymous$$ouse events, as they're mapped to touch. But I don't know if it's the best.

avatar image rob5300 · Jan 18, 2016 at 06:39 PM 1
Share

Just changed the code, tested and it worked for me. The object i tested on had a Box Collider 2D on it. You can really do it eathier way, but having a script on each object to allow them to be touchable is not very efficent.

Having One script that can detect many objects with colliders to me is a much better way. Please try this code out now and see if that works, and remember to change touchphase to suit your needs.

EDIT: This only detects when the finger is removed when over an object with a collider, for your purpose you want to check if the finger was initially touched over the object and is being dragged. Just use the different TouchPhases (Began and $$anonymous$$oved) to check this.

avatar image
7

Answer by Kross_Demon · Apr 26, 2018 at 08:13 PM

Maybe Try this:

 if (Input.touchCount == 1 && Input.GetTouch(0).phase == touchPhase) {
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         RaycastHit hit;

         Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow, 100f);

         if(Physics.Raycast(ray, out hit))
         {
             Debug.Log(hit.transform.name);
             if (hit.collider != null) {
                 
                 GameObject touchedObject = hit.transform.gameObject;
                 
                 Debug.Log("Touched " + touchedObject.transform.name);
             }
         }
  }

touchPhase is set to TouchPhase.Began

A combination from @rob5300 post and https://www.youtube.com/watch?v=0sFrDJKwsdM

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
6

Answer by elblogdelbeto · Jun 23, 2018 at 01:08 AM

better and easy way, just add box collider and a script with this event to your game object:

OnMouseUpAsButton() { }

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 florinel2102 · Aug 16, 2020 at 12:16 AM 1
Share

Yeah , that's works for me . Sometimes I forget to take a step back and think simpler :) . Thanks

avatar image JPBA1984 · Oct 29, 2020 at 10:50 PM 0
Share

While this is the best answer, the collider should be the one that fits your solution.

avatar image
0

Answer by frhilyana118 · Feb 10, 2020 at 09:14 AM

will the debug for raycast touch position response if i use mouse to click on the object during the game view in unity? because I'm trying to test out whether the raycast by touch position is working or not.

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 JPBA1984 · Oct 28, 2020 at 01:17 PM 0
Share

@frhilyana118 questions should be on comments, not answers

avatar image
0

Answer by prakyath_unity · Nov 27, 2020 at 05:58 AM

 #if !UNITY_EDITOR
 if( Input.touchCount>0 )
 {
     foreach( Touch t in Input.touches )
     {
         Debug.LogError($"current touch {Input.touches}");
         Debug.LogError($"touch count is {Input.touchCount}");
         Vector3 touchPos = t.position;
         if( t.phase==TouchPhase.Began )
         {
             Ray ray = Camera.main.ScreenPointToRay( touchPos );
             RaycastHit2D hit = Physics2D.Raycast( ray.origin , ray.direction );
             if( hit.collider!=null && hit.transform==transform )
             {
                 Debug.Log($"Hit: {hit.transform.name}");
                 myOnMouseDown();
                 break;
             }
         }
     }
 }
 #endif
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 IsoRegolith · May 27 at 01:39 PM 0
Share

I am using unity visual scripting, and trying to figure out how to translate the hit.transform == "transform" part into visual script, I put quotes on the part in question.

EDIT:

I actually thought I could do a call to a literal transform (none) but really I just had to check if the hit.transform wasn't null. So now it works, thanks for the nice simple architecture for this.

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

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

Related Questions

Simple cell-based game goes bonkers when clicked on the edge of the cell 0 Answers

Separate Input from Touch on GUI 0 Answers

Is there a way to ensure a cursor position must hover over an object to work in OnMouseDrag? 1 Answer

I want to move my cube on right or left when I touch screen for Android devices; Please help me which script I have to use 0 Answers

Input Touch(Take off finger but it's recognized as if touch was pressed.) 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