Detecting tap on the scene objects.

Ask for help and post bugs.

Detecting tap on the scene objects.

Postby Root » Wed Apr 05, 2017 2:38 pm

I have 2d scene (map) with objects (buildings). Objects have polygonCollider2d.

I have some layers in unity, root object is in "default" layer, buildings in "objects" layer

I need to detect tap on this objects and detect taps on empty map space.
If i tap on empty space i need get false or null in variable value and when if i tap on object with collider i want get it name or tag or etc

My root object
Image

My code
Code: Select all

   public TapGesture objTap;

    private void OnEnable()
    {
        objTap = GetComponent<TapGesture>();
        objTap.Tapped += tappedHandler;
    }

    private void OnDisable()
    {
        objTap = GetComponent<TapGesture>();
        objTap.Tapped -= tappedHandler;
    }

    private void tappedHandler(object sender, System.EventArgs e)
    {
        Debug.Log("in tap");
        var gesture = sender as TapGesture;
        TouchHit hit;
        gesture.GetTargetHitResult(out hit);
        if (/*hitresult &&*/ gesture.GetComponent<PolygonCollider2D>() != null)
        {
            var hitname = gesture.GetComponent<PolygonCollider2D>().name;
            Debug.Log(hitname);
        }
        else {
            Debug.Log("empty space");
        }   
    }



What i should make for solving this problem
Root
 
Posts: 1
Joined: Wed Apr 05, 2017 12:46 pm

Return to Help and Bugs

Who is online

Users browsing this forum: No registered users and 3 guests