Search Unity

Detecting mouse click on object

Discussion in 'Scripting' started by JeffHung, Mar 20, 2009.

  1. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    Hi good people,

    I am new to unity and javascript.
    This question is probably addressed in the past, but I can't find one.

    I'm trying a simple interactive here.
    I have a Cube (with collider naturally).
    I want to click the box with mouse button and make it rotate.
    However I can't seem to get it done correctly.

    I used the code:

    function Update () {
    if (Input.GetKey ("mouse 0")) {
    print ("Box CLICKED!");
    }
    }

    but it works even if I click NOT on the cube.
    I tried with function OnMouseUp but it doesn't seem to work.

    Advice pls :?

    Thanks in advance :)[/code]
     
  2. SpongeBob

    SpongeBob

    Joined:
    Feb 13, 2009
    Posts:
    28
  3. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    WOW! a reply in 3 mins!!

    Thanx for the pointer.
    Yes I have looked through the MonoBehaviour and changed my event from update to OnMouseDown.
    Therefore the script:
    Code (csharp):
    1.  
    2. function OnMouseDown () {
    3.     if (Input.GetKey ("mouse 0")) {
    4.         print ("Box Clicked!");
    5.     }
    6. }
    7.  
    It doesn't seem to work, the print doesn't come out.
    Any idea what did I do wrong?
    Or can give me example?
     
  4. SpongeBob

    SpongeBob

    Joined:
    Feb 13, 2009
    Posts:
    28
    strange:

    Are you sure "mouse 0" is set in the input manager ?
    I Hope you've put the script in the cube gameobject ? ;)
    you can also try to use Input.GetMouseButton(0)
     
  5. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    Wah thanks for the reply so far..
    Hmmm.. So logically the script should work?? :eek:

    Yes I have put the mouse 0 under Fire1 set in the input manager.
    Yes of course I have applied the script onto the Cube.

    Strangely the script works fine with update, but it doesn't detect collision on the Cube. I want to activate the script only when I click the Cube.

    It is a simple scene with one Cube, one Camera and (supposibly) three-lined script.
    What could go wrong :eek:

    Anymore idea?
    :?
     
  6. SpongeBob

    SpongeBob

    Joined:
    Feb 13, 2009
    Posts:
    28
    not sure about "print"
    try Debug.Log instead
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't use GetKey.

    Code (csharp):
    1. function OnMouseDown () {
    2.     print ("Box Clicked!");
    3. }
    --Eric
     
    tkamruzzaman likes this.
  8. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    Well it doesn't work with transform.Translate as well.

    Basically any script under function OnMouseDown doesn't seem to be activated.

    Thanks for the reply.
    I will continue tomorow :)
     
  9. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    Tried that.. not working as well :(
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The only ways I know of that wouldn't work are 1) no collider on the box, 2) something in front of the box that's not on the IgnoreRaycast layer, or 3) the box itself is on the IgnoreRaycast layer.

    --Eric
     
  11. joelmgallant

    joelmgallant

    Joined:
    Mar 9, 2009
    Posts:
    113
    From the code that you posted, it doesn't seem like there's any Raycasting going on to determine what's actually -been- clicked...
    Code (csharp):
    1.  
    2. // Did we hit the surface?
    3. var hit : RaycastHit;
    4. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5. Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
    6.    
    7. if (Physics.Raycast (ray, hit))
    8. {  
    9.     //  Do whatever you want to detect what's been hit from the data stored in the "hit" variable - this should rotate it...
    10.  
    11.    var transform : Transform = hit.collider.GetComponent(Transform);
    12.  
    13.   if (transform )
    14.   {
    15.     transform.localEulerAngles.x += 0.5;
    16.   }
    17.  
    18. }
    19.  

    A good place to start is the "DragRigidBody" script located in the Standard Assets script folder. Also the Mesh Deformation example in the Procedural Examples pack.

    Note that you can also set Layer Masks to ignore certain layers when casting these rays - very useful!
     
  12. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    You don't have to do any of that if the object has a rigidbody of some type and a collider. If you have a collider with matching rigidbody, function OnMouseDown is called every time the mouse clicks on the object.

    If you don't want to do it that way, then you're stuck doing calculations based on the object's Camera.WorldToScreenPosition() and the position of the mouse at mouse-click.

    Note: Be sure to set the rigidbody.useGravity to false, isKinematic to true, and freezeRotation to true if you don't want the cube moving just yet.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You don't need a rigidbody, just a collider. OnMouseDown() uses raycasting; you don't have to write it out yourself. The script I posted is 100% functional as-is. ;) Oh yes, one other way it won't work: 4) You didn't attach the script to the box....

    --Eric
     
    Krish-Vikram likes this.
  14. joelmgallant

    joelmgallant

    Joined:
    Mar 9, 2009
    Posts:
    113
    Wow - I never knew! Thanks guys!

    I guess it's since my use was for basically any object in the world, but this is much more convenient.
     
  15. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    People, try to keep it simple, kinda lost in here :p

    1. Yes, there is a box collider (created by default when a created a cube game object)
    2. There is only 2 objects in the scene: a cube and a camera. Unless the camera is the one blocking the raycast, there is no other object.
    3. I don't really get the IgnoreRaycast layer thingy, but there is no layer created.
    4. Yes of course I have attached the script onto the cube.

    Still not working :(
    Just for info: I'm using the latest release 2.5 for Windows, trial version.

    Is it more helpful if I post the scene file here?? :?

    Thanks!!
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep, because with that setup it should really just work. :)

    --Eric
     
  17. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    Here's the scene file and my js document I extracted from Assets folder.

    Please have a look and thanks for the help :wink:
     

    Attached Files:

  18. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    OH!! FOUND THE PROBLEM!!
    I have been using the project from other version (2.5 beta).

    I don't think the compiler works the same way in the final release.

    I created a new project and copy the Scene and the Script over and it works like magic!!

    Thanks for all the trouble, ignore the previous post.
    This is embarassing :roll:

    Many, many thanks again :)
     
  19. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    HI Jeff,
    My code and project is almost identical to yours. I'm trying to draw my ray in a similar way. When your ray is drawn OnMouseDown, does it stay on screen when OnMouseUp? What if you where to move your camera, does the ray dissapear? I ask because I checked for the hit and it is happening. But the following Debug.DrawLine or Debug.DrawRay does not show on screen.

    I was wondering if this might be because the code is within the OnMouseDown function and not continuously run in the update() function.
     
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes...the line is only drawn for one frame, so if you want to see it, you have to add whatever logic is necessary to make it be drawn longer.

    --Eric
     
  21. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    interesting thing. I placed the Debug.DrawRay in the Update() function and it did not show. I stopped the simulation and the line appeared for a split second before the Unity interface refreshed.

    Could this be a bug?
     
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's a bug, but not a bug in Unity. ;) Your code is doing something wrong.

    --Eric
     
  23. verbatimline

    verbatimline

    Joined:
    Mar 24, 2009
    Posts:
    88
    If I have my interface set up to the standard 4 view panel and I simulate the game, I am able to see the line in the orthographic views. Is it supposed to show in the actual game viewPanel? If it is not, then everything is working fine.
     
  24. JeffHung

    JeffHung

    Joined:
    Mar 20, 2009
    Posts:
    9
    Hi Verbatim,

    For one, I am a totally newbie here. :p
    I'm not sure about your code, but what I managed to do with mine was to create a New Project and copy ONLY the scene and user-built-script over to the new project.

    My guess is that it has something to do with old and new version of the assets/API being different.

    No guarantee but it's worth a try :wink:
     
  25. digitalbee

    digitalbee

    Joined:
    Nov 19, 2014
    Posts:
    2
    Works for me :)
     
  26. will_unity731

    will_unity731

    Joined:
    Aug 16, 2021
    Posts:
    2
    Sorry to revive this ancient thread, but can you get the hit data from an OnMouseDown function? Or is the only way to do that to alternatively use the Physics Raycast and grab the hit data from there?