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
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 hit;
     
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if (Physics.Raycast(ray, out hit))
     {}

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
5
Best Answer

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

For 2D and an Orthographic camera, you can do it like this:

 void Update () {
     Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
     if (hit != null && hit.collider != null) {
         Debug.Log ("I'm hitting "+hit.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

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 ins$$anonymous$$d 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
2

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

why not use this 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

Because this whole screen touch detect not particular object.

avatar image
1

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

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

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

The equivalent in 2D is:

 var ray = Camera.main.ViewportPointToRay(new Vector3(touch.Position.x, touch.Position.y, 0));
 var hit = Physics2D.GetRayIntersection(ray);
 if (hit.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

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

Ins$$anonymous$$d 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.

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

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

Unity APP didn't load on the Chrome browser,Unite didn't load on the Chrome 0 Answers

touch detection within area of collider of gameobject 1 Answer

Object with rigidbody bounces after hit. 0 Answers

Box and circle Collider doesn't work 1 Answer

Retrieve Continuous Touch Coordinate 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