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
2
Question by monkeyThunk · Dec 05, 2013 at 01:46 AM · 2dspriteclick objects4.3

How to detect mouse click on new Unity 4.3 Sprite

I've added a Unity 4.3 Sprite to my scene. I can start it animating with a keypress. But I can't figure out how to detect mouse clicks or finger taps on the sprite.

Comment
Add comment · Show 1
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 rosdi · Jun 01, 2014 at 09:09 AM 0
Share

The only way for this to work, apart from making sure there is Collider component, is to make sure the Z index of the object is on top, even if you are making 2D games. The sorting layer is ignored and Z index take precedence ins$$anonymous$$d.

7 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by komodor · Dec 05, 2013 at 01:50 AM

sprite is gameObject as any others so

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseUp.html

should work

edit: the game object needs to have Collider

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 monkeyThunk · Dec 05, 2013 at 01:59 AM 2
Share

Thanks, it was the collider part that I was missing.

avatar image
7
Wiki

Answer by Linus · Feb 10, 2014 at 09:04 PM

Here is another example.

-Sprite needs to have a 2d collider

-have this script on a game object in scene

-make sure camera is orthographic, they are not when making a new scene even in 2d mode

-if to be used as is, place a prefab into the slot that appears in the inspector

 #pragma strict
 
 var tileSelectionMarker : GameObject;
 
 private var selectorSprite : GameObject;
 
 
 function Start () {
     selectorSprite = Instantiate (tileSelectionMarker, Vector3(0,0, 0), Quaternion.identity);
 }
 
 function Update () {
     if(Input.GetMouseButtonDown(0)){
         var mousePosition : Vector2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         var hitCollider : Collider2D = Physics2D.OverlapPoint(mousePosition);
 
         Debug.Log("mouse pos "+mousePosition.x+" y "+mousePosition.y+" ");    
 
 
         if(hitCollider){
             selectorSprite.transform.position.x = hitCollider.transform.position.x;
             selectorSprite.transform.position.y = hitCollider.transform.position.y;    
             Debug.Log("Hit "+hitCollider.transform.name+" x"+hitCollider.transform.position.x+" y "+hitCollider.transform.position.y);    
         }
         
     
         
     }
 
 }
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
avatar image
1

Answer by Romano · Feb 03, 2014 at 12:45 PM

I wrote a script that does clicks and takes the new sorting layers and sorting order into account, check it out with some long explanation here: http://notquiteblackandwhite.com/post/75474540217/how-to-do-2d-mouse-clicks-in-unity

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 Linus · Feb 11, 2014 at 10:41 PM 0
Share

Direct link to the script http://pastebin.com/Vbzrt0z7 Worth to checkout if you have multiple sprites and need to only click the closest one.

avatar image shopguy · May 14, 2014 at 04:34 AM 0
Share

http://pastebin.com/y8iVHZGr -- updated for my needs, thanks for the good starting point, figured I'd pay it back by sharing my code as well :)

avatar image
0

Answer by rakkarage · May 14, 2014 at 04:36 AM

http://interactivelab.github.io/TouchScript/

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
avatar image
0

Answer by siddharth3322 · May 14, 2014 at 05:14 AM

you have to attach any type of collider to you gameobject.

Other all required detail you can find in my following post.

http://answers.unity3d.com/questions/619090/touch-detection-in-2d-game.html

If you want any other detail then feel free to ask.

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
  • 1
  • 2
  • ›

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

23 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

Related Questions

Sprite's alpha visible with lighting 0 Answers

Change sprite when click on particular sprite 1 Answer

How to put multiple sprites in one click inside of a game object? 0 Answers

2D Level Design (programmatically or not) 1 Answer

Animataion of a Sprite v2 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