Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Detecting touch of specific GameObject

Discussion in 'iOS and tvOS' started by bangtickboom, Aug 4, 2009.

  1. bangtickboom

    bangtickboom

    Joined:
    Jun 26, 2009
    Posts:
    26
    Hello everyone,

    I've been working diligently on my game for about a month or so, learning Unity along the way, and just recently purchased Unity iPhone. What a great product!

    Okay, so to my point.

    The puzzle game I'm working on allows the player to click on specific game objects, they act as buttons basically. I have the following script that I'm using to detect if a specific game object was "hit".

    Is this the proper way to do this for iPhone? I'm a programmer by trade, but still getting familiar with game programming and Unity scripting.

    Code (csharp):
    1.  
    2. var hit : RaycastHit;
    3. var object : GameObject;
    4.  
    5. function Update () {
    6.    
    7.     // Use Raycast to pick objects that have mesh colliders attached.
    8.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    9.    
    10.     //if the user touches the button
    11.     if (Input.GetMouseButtonUp (0))  //Returns true during the frame the user deselects the object
    12.     {
    13.         if (Physics.Raycast (ray, hit, 100)  hit.transform.name == object.name)
    14.         {
    15.             ButtonTouchUp(object);
    16.         }
    17.     }
    18.  
    19. }
    20.  
    This code works properly, but I did notice through my testing that no matter which button I actually click on the game acts as though all buttons were clicked. From the script reference this is normal for raycasting, correct?

    I just want to make sure I'm not missing something simple...I feel like I am.

    Thanks in advance for any suggestions or comments!
     
    sbagchi likes this.
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Well the first finger is always mouse button 0.
    thought I would recommend to consider using touches instead and the iPhoneInput class, instead of using the desktop targeted Input class
     
  3. bangtickboom

    bangtickboom

    Joined:
    Jun 26, 2009
    Posts:
    26
    Thanks Dreamora for your reply.

    I guess I can change the code to use touches now that I kind of understand what's going on, that's a good point. :)
     
  4. Tetrad

    Tetrad

    Joined:
    Jul 22, 2009
    Posts:
    45
    If you use collider.Raycast instead of Physics.Raycast you don't have to do the name check.

    collider.Raycast only tests for the collider of whatever gameobject you have that script bound to.

    For example, put this in your Update():
    Code (csharp):
    1.         if( iPhoneInput.touchCount >= 1 )
    2.         {
    3.             Ray cursorRay = Camera.main.ScreenPointToRay( iPhoneInput.GetTouch(0).position );
    4.             RaycastHit hit;
    5.             if( collider.Raycast( cursorRay, out hit, 1000.0f ) )
    6.             {
    7.                 Debug.Log( "Hit detected on object " + name + " at point " + hit.point );
    8.             }
    9.         }
    10.  
     
    sbagchi and ghulamustafa like this.
  5. bangtickboom

    bangtickboom

    Joined:
    Jun 26, 2009
    Posts:
    26
    Tetrad, thanks so much! That's really what I was looking for, didn't even know about using collider.Raycast instead. All of the examples I was able to find were using Physics.Raycast.

    That's awesome! :D
     
  6. MugOfPaul

    MugOfPaul

    Joined:
    Jun 13, 2008
    Posts:
    34
    Unless I'm misunderstanding the suggestion of using collider.Raycast, you probably want to stick with doing Physics.Raycast and putting your "touchable" objects on a layer of their own. That way, you only run the raycast test once for the whole group of objects instead of each object doing a raycast itself. Then again, the performance difference may be negligible. :?:
     
  7. planktons

    planktons

    Joined:
    Nov 6, 2011
    Posts:
    3
    @Tetrad , Thank very much for your answer.
     
  8. octaviomejiadiaz

    octaviomejiadiaz

    Joined:
    Jul 9, 2012
    Posts:
    2
    how to do this on c++?