• 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
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
motazmmnshob

People who like this

1 Show 0
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
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
Vitaliq
LoyalRayne
owlhoot
sadraquejsa
motazmmnshob
sifrani

People who like this

6 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

Thanks for your answer, I will check this code and reply to you shortly.

avatar image Foteini · Mar 20, 2014 at 01:42 PM 0
Share

Thank you @robertbu! :)

avatar image Esser420 · Jul 12, 2014 at 02:42 PM 0
Share

If I want a android touch position, what do I put instead of the Input.mousePosition?

I have already tried: - Input.GetTouch(0).position - new Vector3(touch.position.x , touch.position.y, 0);

avatar image siddharth3322 · Jul 14, 2014 at 04:45 AM 0
Share

you have to use Camera.main.ScreenToWorldPoint (Input.mousePosition); to get touch position.

avatar image

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
motazmmnshob

People who like this

1 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

If I want to convert my statements for 2d games then what will be?Because all classes are available in 2d.

avatar image JoaoOliveira · Jan 18, 2014 at 11:46 AM 0
Share

Instead of:

 if (Physics.Raycast(ray, out hit))

You use:

 if (Physics2D.Raycast(ray, out hit))
avatar image siddharth3322 · Jan 18, 2014 at 02:42 PM 0
Share

You mislead in this thing there is no function available in Physics2D.

avatar image JoaoOliveira · Jan 18, 2014 at 02:58 PM 0
Share

Ok I didn't remember it wasn't exactly the same.

But in any case, it's the same principle as in 3D: if you check the input arguments in the link I provided you should be able to adapt your code accordingly.

avatar image

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
GamdineProductions
motazmmnshob

People who like this

2 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

Because this whole screen touch detect not particular object.

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

touch detection within area of collider of gameobject 1 Answer

Object with rigidbody bounces after hit. 0 Answers

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

Box and circle Collider doesn't work 1 Answer

How to hit two object with one raycast? 2 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