• 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
1
Question by siddharth3322 · Jan 17, 2014 at 11:58 AM · colliderunity 2dtouchunity4.3

Touch Detection in 2D Game

Using following code I can able to detect touch of 3d box collider but when I attach Box Collider 2D, the code does not work.

     RaycastHit $$anonymous$$t;
     
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if (Physics.Raycast(ray, out $$anonymous$$t))
     {}

So how to detect touch for Box Collider 2D?

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

3 Replies

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

Answer by robertbu · Jan 18, 2014 at 05:45 PM

For 2D and an Orthograp$$anonymous$$c camera, you can do it like t$$anonymous$$s:

 void Update () {
     Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     RaycastHit2D $$anonymous$$t = Physics2D.Raycast(pos, Vector2.zero);
     if ($$anonymous$$t != null && $$anonymous$$t.collider != null) {
         Debug.Log ("I'm $$anonymous$$tting "+$$anonymous$$t.collider.name);
     }
 }

There is another line or two if you are using a Perspective camera.

Comment
Add comment · Show 4 · 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 siddharth3322 · Jan 18, 2014 at 06:11 PM 0
Share
avatar image Foteini · Mar 20, 2014 at 01:42 PM 0
Share
avatar image Esser420 · Jul 12, 2014 at 02:42 PM 0
Share
avatar image siddharth3322 · Jul 14, 2014 at 04:45 AM 0
Share
avatar image
2

Answer by vfxjex · Sep 17, 2014 at 08:50 AM

why not use t$$anonymous$$s instead?

JavaScipt

 function OnMouseOver () {
    if(Input.GetMouseButtonDown(0)){
    //your code
    }
 }
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 siddharth3322 · Sep 17, 2014 at 09:12 AM 1
Share
avatar image
1

Answer by JoaoOliveira · Jan 17, 2014 at 02:24 PM

Ok, I recently had to implement t$$anonymous$$s very same functionality. I had code for 3D colliders and didn't want to change it too much. So in 3D I was doing t$$anonymous$$s:

 var ray = Camera.main.ViewportPointToRay(new Vector3(touch.Position.x, touch.Position.y, 0));
 RaycastHit $$anonymous$$t;
 if (Physics.Raycast(ray, out $$anonymous$$t)){
     //Do stuff
 }

The equivalent in 2D is:

 var ray = Camera.main.ViewportPointToRay(new Vector3(touch.Position.x, touch.Position.y, 0));
 var $$anonymous$$t = Physics2D.GetRayIntersection(ray);
 if ($$anonymous$$t.collider != null) {
     //Do stuff
 }
Comment
Add comment · Show 4 · 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 siddharth3322 · Jan 18, 2014 at 05:17 AM 0
Share
avatar image JoaoOliveira · Jan 18, 2014 at 11:46 AM 0
Share
avatar image siddharth3322 · Jan 18, 2014 at 02:42 PM 0
Share
avatar image JoaoOliveira · Jan 18, 2014 at 02:58 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Rotate a 2D object slowly to the touch position 0 Answers

touch detection within area of collider of gameobject 1 Answer

Object with rigidbody bounces after hit. 0 Answers

Random Object movement 2 Answers

Box and circle Collider doesn't work 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