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
12
Question by noob101 · Nov 17, 2013 at 12:00 PM · touchunity 4.3

How to detect a touch on Box Collider 2d in Unity 4.3

I am familiar with the 3D way of doing this

 Ray ray = Camera.mainCamera.ScreenPointToRay(Input.GetTouch (0).position);
 if (Physics.Raycast(ray, out hit)) 

But when I have a Box Collider 2D , the Ray doesn't seem to hit, I tried using the Physics2D.Raycast but cannot get it also does not hit because its 2D ?

Thanks for any hints

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

Answer by noob101 · Nov 19, 2013 at 01:24 PM

Finally I managed to find the answer , in case somebody else who is not new to unity needs this the answer is

 void Update()
 {
     if (Input.touchCount == 1)
     {
         Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
         Vector2 touchPos = new Vector2(wp.x, wp.y);
         if (collider2D == Physics2D.OverlapPoint(touchPos))
         {
             //your code
 
         }
     }
 }
Comment
Add comment · Show 17 · 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 immeasur · Nov 20, 2013 at 09:33 PM 0
Share

Thanks for sharing. I was using the same method in 3D and got stuck when I tried with 2D.

avatar image immeasur · Nov 21, 2013 at 05:40 AM 0
Share

How do you access that colliding object? Like if I wish to change its color or destroy it?

avatar image noob101 · Nov 21, 2013 at 10:18 AM 0
Share

This script is ideally attached to the object so you will have direct access, but for other collisions you have to use OnCollisionEnter2D , see this http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionEnter.html

avatar image immeasur · Nov 21, 2013 at 02:43 PM 0
Share

I'm trying to run a foreach loop through touches in a separate script. I need to access the objects that I tap and change color. Could you help me out?

avatar image xeratol · Dec 01, 2013 at 01:46 PM 2
Share

You can further simplify that since Vector3D inherits from Vector2D

 if (Input.touchCount > 0)
 {
     Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
     if (collider2D.OverlapPoint(wp))
     {
         //your code
         Debug.Log ("Hello");
     }
 }
 
Show more comments
avatar image
0

Answer by Cervelx · Jun 04, 2017 at 10:02 PM

Hi thank you for the code, i have a question for you, with this script if i continue to press the screen the code is "repeating", the is a way to stop this, i mean i want to repeat my code but i want to tap again, i want to avoid that whit pressure the code is repeating...

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 PraetorBlue · Mar 19, 2018 at 03:18 AM 0
Share

Input.GetTouch() returns a Touch struct. If you want to only do something during the first frame in which the touch starts, check for the phase of the touch. you probably want TouchPhase.Began. See https://docs.unity3d.com/ScriptReference/Touch-phase.html

For example:

  void Update()
  {
      if (Input.touchCount == 1)
      {
          Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
          Vector2 touchPos = new Vector2(wp.x, wp.y);
          if (collider2D == Physics2D.OverlapPoint(touchPos) && Input.GetTouch(0).phase == TouchPhase.Began))
          {
              //your code
  
          }
      }
  }

avatar image
0

Answer by Akusan · Feb 02, 2020 at 04:32 AM

In case anyone is wondering what this should be for Unity 2019.3, 1st make sure the Game Object has a 2D Collider component, then add the following script:

 void Update()
 {
     if (Input.touchCount > 0)
     {
         Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
         if (GetComponent<Collider2D>().OverlapPoint(wp))
         {
             //your code
             Debug.Log("Hello");
         }
     }
 }
Comment
Add comment · 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

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

33 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

Related Questions

Is it possible to detect which side of a sprite a touch came in from? 0 Answers

Lean Joystick gets stuck on disable 0 Answers

Why do I need to use a halo component in 4.3? 1 Answer

unity 4.3 sprite script 0 Answers

Polygon collider 2D is not fitting sprite correctly 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