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
0
Question by IamMai · Jan 13, 2014 at 06:15 PM · mouseposition

How to check if click mouse on object

Hi! I want to check if player click mouse on object, movie will play. I put Input.GetMouseButton (0) function on the object but if click position is not in object,this script will run too

Please tell me how to to this and I want to know how to check mouse position in "Object area", I know about use Raycast to get mouse position but I don't know how to check if my mouse position is in my object area, how to do this? Thanks!

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

Answer by Sundar · Jan 13, 2014 at 06:27 PM

Make sure your object has a collider then try this

 function Update () {
         if (Input.GetMouseButtonDown(0)) {
             var hit: RaycastHit;
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, hit)) {
                 if (hit.transform.name == "MyObjectName" )Debug.Log( "My object is clicked by mouse");
             }
         }
Comment
Add comment · Show 7 · 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 IamMai · Jan 13, 2014 at 06:35 PM 0
Share

Wow It's work! Thank you very much :)

avatar image Sundar · Jan 13, 2014 at 06:38 PM 0
Share

You are welcome

avatar image Stephanie_Brooks · May 02, 2017 at 06:06 PM 0
Share

Thanks for posting this! How would this work in c#? also, how would you do this if you wanted to check if the object that was hit had a certain tag on it?

avatar image averysmarttardigrade Stephanie_Brooks · Jan 18, 2020 at 04:51 PM 0
Share

@Stephanie_Brooks ins$$anonymous$$d of using hit.collider.name, replace the .name with .tag.

avatar image toddisarockstar · Oct 16, 2017 at 02:34 AM 3
Share

C# translation

     void Update () {
              if (Input.Get$$anonymous$$ouseButtonDown(0)) {
                 RaycastHit  hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                  
                  if (Physics.Raycast(ray, out hit)) {
                      if (hit.transform.name == "$$anonymous$$yObjectName" )
 {print( "$$anonymous$$y object is clicked by mouse");}
                  }
              }
avatar image bobwithacamera · Apr 20, 2019 at 01:24 AM 1
Share

Is there a way to do this on mobile?

Show more comments
avatar image
10

Answer by nbg_yalta · Jan 13, 2014 at 06:26 PM

You can use OnMouseDown or raycast check. here is onmousedown example:

 void OnMouseDown() 
 {
    //Do something
 }

Be sure you have collider on your object.

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 IamMai · Jan 13, 2014 at 06:36 PM 0
Share

Oh I for got that we have On$$anonymous$$ouseDown function thank you

avatar image bekranker · Mar 03, 2021 at 03:22 PM 0
Share

But this function is working for just him's Component

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

28 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

Related Questions

Getting weird results when subtracting two identical numbers 0 Answers

Issues with mouse moving camera 0 Answers

Convert Mouse Position to move a cube on 1-axis. 1 Answer

2d mouseposition in a 3d world 3 Answers

How to detect if mouse is over GUI element in Web Player? 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