Search Unity

Touching a 2d sprite?

Discussion in '2D' started by Pandamonium99, Mar 11, 2014.

  1. Pandamonium99

    Pandamonium99

    Joined:
    Jul 16, 2012
    Posts:
    14
    Im trying to code a start button for a 2d game for android but I cannot figure it out. My code currently is:

    if (Input.touchCount == 1)
    {
    Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    Vector2 touchPos = new Vector2(wp.x, wp.y);

    if (collider2D.OverlapPoint(touchPos))
    {
    //your code
    //Application.LoadLevel("RunLevel");
    msg = Physics2D.OverlapPoint(touchPos).gameObject.transform.tag;
    }
    }

    This should be checking going to the next scene if i press the play button, but if i press anywhere on the screen its returning the collider its attached to.
     
  2. Ultroman

    Ultroman

    Joined:
    Mar 10, 2014
    Posts:
    110
    I'd recommend using raycasts instead, and some better touch-handling, to prevent multitouches (taken from the Unity script reference).

    Code for touches (cannot test it myself, though):
    Code (csharp):
    1.  
    2.     for (var i = 0; i < Input.touchCount; ++i) {
    3.         if (Input.GetTouch(i).phase == TouchPhase.Began) {
    4.             RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero);
    5.             // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
    6.             if(hitInfo)
    7.             {
    8.                 Debug.Log( hitInfo.transform.gameObject.name );
    9.                 // Here you can check hitInfo to see which collider has been hit, and act appropriately.
    10.             }
    11.         }
    12.     }
    13.  
    ...but I tested this for use with mouse-clicks, and it works perfectly.
    Code (csharp):
    1.  
    2.     if (Input.GetMouseButtonDown (0)) {
    3.         Debug.Log ("Clicked");
    4.         Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    5.         RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
    6.         // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
    7.         if(hitInfo)
    8.         {
    9.             Debug.Log( hitInfo.transform.gameObject.name );
    10.             // Here you can check hitInfo to see which collider has been hit, and act appropriately.
    11.         }
    12.     }
    13.  
    Just remember that your sprite must have some kind of 2D collider on it to be registered.
     
  3. Pandamonium99

    Pandamonium99

    Joined:
    Jul 16, 2012
    Posts:
    14
    It's still not working, no matter where I touch on the screen it always returns with the play button? Its weird, the play button is the only box collider on the screen and if I click say the background it returns "playbutton" instead of null.... so weird! If the sprite box collider's size is less then 1 it will only return the backgrounds collider, if its over size of 1 and it only returns the box collider on the play button.
     
    Last edited: Mar 11, 2014
  4. Pandamonium99

    Pandamonium99

    Joined:
    Jul 16, 2012
    Posts:
    14
    It's like the ray is off or something, if the boundary for the playbutton is over 1 then even if I click outside the collider it returns play button!? If my x scale on box collider is over 1.5, no matter where i press on the screen, it always returns the playbutton, and if I do under 1.3 it doesnt detect anything, even if im right ontop of it.
     
    Last edited: Mar 11, 2014
  5. Pandamonium99

    Pandamonium99

    Joined:
    Jul 16, 2012
    Posts:
    14
    Yup, I'm officially dumb... haha. I forgot to turn my camera to orthographic for 2d games! DOH! Thanks a ton ultro the code works perfectly.
     
  6. Ultroman

    Ultroman

    Joined:
    Mar 10, 2014
    Posts:
    110
    Heheh, happens to the best :)

    And you're very welcome. It's a 3-minute mix-match of 2 different pages in the Unity Script Reference, so it darn well should work ;)

    Remember that local variables, like that Vector2 pos and the hitInfo, would probably be better off as a global temp-variable, if you're running this script in an Update() method. If the code is very situational, it should be fine the way it is.
     
    Last edited: Mar 12, 2014
  7. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    123
    do we write that function on update?
     
  8. dangalg

    dangalg

    Joined:
    Oct 2, 2014
    Posts:
    1
     
  9. kaancetinkayasf

    kaancetinkayasf

    Joined:
    Feb 20, 2020
    Posts:
    10
    Thanks for the code works perfectly!
     
    Ultroman likes this.