Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Muscovod · Sep 13, 2015 at 09:37 AM · collisionraycastraycasthitmouseclick

How do you detect a mouse button click on a Game Object? C#

Hello,

I was making a 2D platformer in which you could press the "retry" button to restart the game. I looked around for a few guides but they all seemed to either be in javascript, or were outdated. I read a specific guide which tells you to use raycasts in order to detect whether or not a gameobject is clicked. However, my code did not work.

 void Update (){
     if (Input.GetMouseButtonDown (0)) {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

         if(Physics.Raycast (ray, hit))
         {
             if(hit.transform.name == "Player")
             {
                 Debug.Log ("Logged");
             }
         }
     }
 

What is wrong with this and how can it be fixed? I am aware that the variable "hit" is nothing at the moment, but I do not know how to assign raycasthit variables. Any help would be greatly appreciated.

Also, it seems that the syntax for Physics.Raycast changed, or I do not understand it properly. In the scripting API it says Raycast(Vector3 origin, Vector3 direction), but most guides on the internet use "hit" as their second parameter. If anyone could explain this, it would be appreciated.

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

Answer by Nikunj-Kareliya · Sep 13, 2015 at 10:51 AM

Make sure, you use 'out hit' as a argument, in C#.

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 
 if(Physics.Raycast (ray, out hit))
 {
      if(hit.transform.name == "Player")
      {
          Debug.Log ("This is a Player");
      }
      else {
          Debug.Log ("This isn't a Player");                
      }
 }


PS. use tags instead of compare game object names !

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 Nikunj-Kareliya · Sep 13, 2015 at 10:57 AM 0
Share

As you are developing 2D game use 'Raycast2D' ins$$anonymous$$d of 'Raycast'.

Raycast2D

$$anonymous$$ore better way is to use Unity UI for buttons like things..

avatar image
3

Answer by shanta3220 · Feb 25, 2018 at 07:25 AM

Although this is late, if anybody is curious of how to do it with RaycastHit2D for your 2D game. (make sure to set main camera to Orthographic).

 RaycastHit2D hit = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x, Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y), Vector2.zero, 0);
 if (hit) {
     if (hit.collider.CompareTag("Player")) {
             Debug.Log("This is player");
 }


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 shanta3220 · Feb 04, 2019 at 05:05 PM 1
Share

Another 2D Solution is to use OverlapPoint:

 private void Update(){
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         Vector3 pos = Input.mousePosition;
         Collider2D hitCollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
         if (hitCollider != null && hitCollider.CompareTag ("Player")) {
              Debug.Log("This is player");
         }
   }
avatar image staryzhelios shanta3220 · May 08, 2019 at 04:07 PM 0
Share

not working

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast not detecting hit 4 Answers

How to freeze an GameObject on x axis in specific direction? 1 Answer

Adding collision to Raycast hit 0 Answers

Make raycast ignore hitbox? 0 Answers

Restrict held object movement 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