Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nicoolai · Jul 10, 2018 at 03:09 PM · 2dspritetouch

How to detect a touch on a 2D sprite

Hi, I realize there's a lot of questions for this topic already but none of them really fixed this for me. Maybe I'm just doing it completely wrong, I am very much a noob with unity.

Anyway, I have a 2D sprite on which I have added an Edge collider 2D. I'm trying to detect when I touch it, on my touch device.

I tried to follow another post I found here, but it never registers a touch. Here's the code I have so far.

     private Collider2D collider;
 
     // Use this for initialization
     void Start()
     {
         collider = GetComponent<Collider2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         CheckForTouch();
     }
 
     bool CheckForTouch()
     {
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
         {
 
             var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
             var touchPosition = new Vector2(wp.x, wp.y);
 
             if (collider == Physics2D.OverlapPoint(touchPosition)){
                 Debug.Log("HIT!");
             }
             else{
                  Debug.Log("MISS");
             }
         }
         return false;
     }
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

2 Replies

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

Answer by Chik3r · Jul 10, 2018 at 03:23 PM

Maybe the edge collider isn't working (You have to touch the edge?), try using a polygon colider

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 Nicoolai · Jul 10, 2018 at 03:53 PM 0
Share

Oh wow, that sounds likely. I thought the edge collider was what I wanted but now that I read you comment it all makes sense. Hah. Thanks, of course the edge collider is for edge only. I feel like a moron.

I'll check if this is it, when I get back home, later today.

avatar image vbvbnbbbj Nicoolai · Nov 30, 2020 at 03:06 PM 0
Share

can you put script when i touch 2d collider the animation state change and when not the animation state return to the default

avatar image Nicoolai · Jul 11, 2018 at 07:46 AM 0
Share

Well, this was totally it. Thanks man :)

avatar image Nicoolai · Jul 17, 2018 at 09:02 AM 0
Share

So, this was pretty much the answer. If you want to put it in as an answer, ins$$anonymous$$d of a comment, I can mark it.

avatar image
1

Answer by Bubinga_Studios · Jul 10, 2018 at 03:33 PM

@Nicoolai If you are going for a UI perspective, you can use the Button component and use the On Click () function to state what you would like it to do (NOTE: The void function you are calling via the button must be a public void ).

Otherwise, if you are using sprites and want to recieve input when target sprite is touched (from what I am seeing that is what you want your code to do), you may want to look into void OnMouseDown() function and do something like this.

void OnMouseDown() { Debug.Log("HIT!"); }

More can be read about OnMouseDown() here. You can also check OnMouseUp() so the target function is only called when you release your mouse button https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

If I did not answer your question the way you were looking for, just reply asking whatever I missed :)

Hope this helped!

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 Nicoolai · Jul 10, 2018 at 03:51 PM 0
Share

Thank you very much, i'll give that a shot when I get home later today.

Regarding the On$$anonymous$$ouseDown(), will that work with touch, ins$$anonymous$$d of a mouse? Or only mouse clicks?

avatar image Bubinga_Studios Nicoolai · Jul 16, 2018 at 11:21 PM 0
Share

Sorry for late response. On$$anonymous$$ouseDown() will only work with mouse clicks. If you want to see if one collider comes in contact with another, try out one of these

For Colliders (2D): void OnCollisionEnter(Collision2D col) calls script only once when collision occurs void OnCollisionStay(Collision2D col) calls script every frame while in collision void OnCollisionExit(Collision2D col) calls script only once when collision ends

For Triggers (2D) void OnTriggerEnter(Collider2D col) calls script only once when entering trigger void OnTriggerStay(Collider2D col) calls script every frame while in trigger void OnTriggerExit(Collider2D col) calls script only once when object leaves trigger

So for example: if I wanted to find out if I collided with an object tagged "Enemy" and it IsAlive (A boolean in a script called $$anonymous$$onster) by finding the collision: void OnCollisionEnter(Collision2D col) { if(col.gameObject.tag == "Enemy" && col.GetComponent<$$anonymous$$onster>().IsAlive) { //Do whatever is needed here :) } } Again, if this wasn't clear, you can check the documentation here:

NOTE: The Collider/Collision does not need to be named "col" it can be whatever you want. SORRY ABOUT LINE SPACING!

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

210 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 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 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

Finding the centre of two touches 2 Answers

Unity2D touch screen input 0 Answers

2 fingers sprite rotation 1 Answer

Custom shader of sprite results in black alpha. 0 Answers

Stop Moving When Not Touching Screen? 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