How to detect swipe across a collider?

Hi I am touching the screen and then swiping across screen to detect the colliders on the screen.The problem when I swipe the colliders are not detected.When I touch on the collider and thenn swipe across the collider it is detected.
Normally when our touch hits the boxcollider2D it should detect.

 if(Input.touchCount > 0)
        {
          
            if(Input.GetTouch(0).phase==TouchPhase.Began )
            {


                    Debug.Log("Touched Screen");
                

            }
            if(Input.GetTouch(0).phase==TouchPhase.Moved && hit.collider.tag == "TouchLines")
            {
              
                    Debug.Log("Touched Line containing collider");
                    audiosource.Play();

            }
                

        }

I would do something like this:

  1. Create a way to detect if the player is swiping. I will assume you know how to do this.
  2. Once you detected that the swiping has started, set the value of some variable (ex. “bool isSwiping”) to true.
  3. Once you detected that the swiping has stopped, set the value of that same variable to false.
  4. In your Update method (or wherever you want to check this), check if the cursor is over the game object and if the variable isSwiping is true. That would mean that at the point of contact between your mouse cursor and your object, the player is definitely swiping.

Use PhysicsRaycaster and the modern event system. You can use IBeginDragHandler.

1 Like