Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
6
Question by tenthplanet0 · Jan 03, 2013 at 06:43 PM · mouse click

how to detect mouse click on a gameobject.

I have an assignment and I am lacking idea in this regard. I am surely a new bee and eagerly waiting for the help.

http://www.gamefools.com/onlinegames/free/supercollapsepuzzlegallery2.html

Please see the game

By looking at this game I got two ideas, one is to use cubes(with rigidbody and colliders attached) and other is to use the GuiTexture. If using cubes, I am think that how to detect the mouse click on individual cube and if using GuiTexture than how to know whether something is below or not.

Please help me

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

7 Replies

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

Answer by Pysassin · Jan 03, 2013 at 09:56 PM

As far as the cube method goes you can use a simple mouse over function and ignore the raycasting entirely. look something like...

 function OnMouseOver(){
    if(Input.GetMouseDown(0){
       // Whatever you want it to do.
    }
 }

That is for JS though the method could easily be ported to C# as well.

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 tenthplanet0 · Jan 04, 2013 at 07:24 AM 1
Share

Pysassin thanks.... your code really worked....

But one thing is that its not Input."Get$$anonymous$$ouseDown" but Input."Get$$anonymous$$ouseButtonDown"

Anyway lots of thanks

avatar image olehsmazhnov · Nov 17, 2013 at 01:08 PM 1
Share

u forgot ")" after "if(Input.Get$$anonymous$$ouseDown(0)" so it should be ... if(Input.Get$$anonymous$$ouseDown(0) ) ... thanks

avatar image Gaming-Dudester · Sep 27, 2015 at 04:11 PM 0
Share

Hey how do u make it to where it only works on a certain tag?

avatar image Jeffdibson · May 14 at 11:09 PM 0
Share

I had the same question, and this worked for me. Here's the C# version: void OnMouseOver() { if (Input.GetMouseButtonDown(0)){ //whatever you want to do here } }

avatar image
4

Answer by sampathcse16 · Oct 29, 2019 at 07:47 PM

You can use the below script to identify the mouse click on particular game object.

 void Update()
  {
         //Check for mouse click 
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit raycastHit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out raycastHit, 100f))
             {
                 if (raycastHit.transform != null)
                 {
                    //Our custom method. 
                     CurrentClickedGameObject(raycastHit.transform.gameObject);
                 }
             }
         }
  }
 
 public void CurrentClickedGameObject(GameObject gameObject)
 {
     if(gameObject.tag=="something")
     {
     }
 }
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
3

Answer by Maulik2208 · Jan 04, 2013 at 08:41 AM

 void OnMouseUp()
 {
   /*Do whatever here as per your need*/
 }
 
 As well as you can use 
  
 void OnMouseDown()
 {
   /*Do your stuff here*/
 }

USe this link to get more info regarding mouse related function---->link text

If found useful then don't forget to mark the answer.......

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 raptorkwok · Oct 27, 2014 at 09:26 AM 0
Share

It requires Collider to be added to Game Object beforehand

avatar image
2

Answer by shigidaMark · Jan 03, 2013 at 06:53 PM

This will check each frame if there is something under the mouse. Then it checks if it is a cube (via tag). If so you could manipulate the cube using hit.collider.gameObject (such as changing it's forward/backward position, size, material, etc.):

 function Update(){
      var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      var hit : RaycastHit;
      if(Physics.Raycast(ray,hit)){
           if(hit.collider.tag == "clickableCube"{
                //hit.collider.gameObject now refers to the 
                //cube under the mouse cursor if present
           }
      }
 }

As for the GUITexture, you can just use that in the OnGUI function to replace the mouse cursor:

 var crosshair : Texture;
 function Start(){
      Screen.showCursor = false;
 }
 function OnGUI(){
      var pos = Input.mousePosition;
      GUI.DrawTexture(Rect(pos.x-crosshair.width/2,pos.y-crosshair.height/2,crosshair.width,crosshair.height),crosshair);
 }
Comment
Add comment · Show 3 · 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 tenthplanet0 · Jan 03, 2013 at 07:19 PM 0
Share

Can you show me how can I make this game using only GuiTexture? Please shigda$$anonymous$$ark.... and lot of thanks Sir

avatar image tenthplanet0 · Jan 03, 2013 at 07:43 PM 2
Share

I am getting this error message for hit variable...

Use of unassigned local variable `hit'

avatar image raptorkwok · Oct 27, 2014 at 09:29 AM 0
Share

the code won't even compile. Failed.

avatar image
1

Answer by lemonyama · Jul 29, 2015 at 03:36 PM

Updated script for this which should work okay:

 if (Input.GetMouseButtonDown (0)) {    
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 100)) {
                 // whatever tag you are looking for on your game object
                 if(hit.collider.tag == "Trigger") {                         
                     Debug.Log("---> Hit: ");                        
                 }
             }    
         }
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 TheNewNerd · Jan 10, 2017 at 07:03 AM 0
Share

Use " var ray = Camera.main.ScreenPointToRay(Input.mousePosition);" ins$$anonymous$$d of " var ray = Camera.current.ScreenPointToRay(Input.mousePosition);" I got an error with the second line of code but using "main" got rid of the error.

  • 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

25 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

Related Questions

How to make a mouse click selection for in game use 1 Answer

How to do a Burst Particle effect on Mouse Click? 2 Answers

Unity2D problem with negative z rotation shooting bullet towards mouse click 1 Answer

How to different hold and one click left button mouse? 1 Answer

Using "Vector3.MoveTowards" to translate an object to mouse click 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