• 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
0
Question by Romano · Jan 16, 2014 at 09:07 AM · mouseclickray4.3

Will mouse click detector demo translate to 2D?

Here is the code I found in a mouse click detector demo for use with 3D box colliders:

 using UnityEngine;
 using System.Collections;
  
 public class ClickDetector : MonoBehaviour
 {
     public bool HandleLeftClick = true;
     public bool HandleRightClick = true;
     public bool HandleMiddleClick = false;
     public string OnLeftClickMethodName = "OnLeftClick";
     public string OnRightClickMethodName = "OnRightClick";
     public string OnMiddleClickMethodName = "OnMiddleClick";
     public LayerMask layerMask;
  
     void Update()
     {
         GameObject clickedGmObj = null;
         bool clickedGmObjAcquired = false;
  
         // Left click
         if (HandleLeftClick && Input.GetMouseButtonDown(0))
         {
             /*if (!clickedGmObjAcquired)
             {*/
                 clickedGmObj = GetClickedGameObject();
                 clickedGmObjAcquired = true;
             /*}*/
  
             if (clickedGmObj != null)
                 clickedGmObj.SendMessage(OnLeftClickMethodName, null, SendMessageOptions.DontRequireReceiver);
         }
  
         // Right click
         if (HandleRightClick && Input.GetMouseButtonDown(1))
         {
             if (!clickedGmObjAcquired)
             {
                 clickedGmObj = GetClickedGameObject();
                 clickedGmObjAcquired = true;
             }
  
             if (clickedGmObj != null)
                 clickedGmObj.SendMessage(OnRightClickMethodName, null, SendMessageOptions.DontRequireReceiver);
         }
  
         // Middle click
         if (HandleMiddleClick && Input.GetMouseButtonDown(2))
         {
             if (!clickedGmObjAcquired)
             {
                 clickedGmObj = GetClickedGameObject();
                 clickedGmObjAcquired = true;
             }
  
             if (clickedGmObj != null)
                 clickedGmObj.SendMessage(OnMiddleClickMethodName, null, SendMessageOptions.DontRequireReceiver);
         }
     }
  
     GameObject GetClickedGameObject()
     {
         // Builds a ray from camera point of view to the mouse position
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
  
         // Casts the ray and get the first game object hit
         if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
             return hit.transform.gameObject;
         else
             return null;
     }
 }

I'm trying to convert this for use with Sprites and 2D Box Colliders - without any luck. There are ideas online for different methods of detecting mouse clicks, but I'm keen to use something as close to this as possible. Does anyone have any suggestions?

Thanks very much! Romano

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

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

I've taken the answer below and added a lot more code to include sorting layers and sorting order. I can't believe how complicated it is. I've posted the script here: http://notquiteblackandwhite.com/post/75474540217/how-to-do-2d-mouse-clicks-in-unity

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 robertbu · Jan 16, 2014 at 09:16 AM

I did not see anything beyond GetClickedGameObject() that had to change for 2D. It can be rewritten as:

 GameObject GetClickedGameObject() {
     Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     RaycastHit2D hit = Physics2D.Raycast (pos, Vector3.zero);

     if (hit != null && hit.collider != null) 
         return hit.collider.gameObject;
     else
         return null;
 }

Note this code assumes an Orthographic camera.

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 Romano · Jan 17, 2014 at 04:29 AM 0
Share

Thanks for that :)

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

18 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

Related Questions

Can I change GUIButton behaviour? 1 Answer

Convert vector in world to mesh 1 Answer

Remove a part of image on mouse click ? 1 Answer

Activate gameobject upon mouseclick 1 Answer

mouse Down+Hold and double click events 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