Search Unity

How to detect swipe across a collider?

Discussion in 'Scripting' started by zyonneo, Sep 20, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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.
    Code (CSharp):
    1.  if(Input.touchCount > 0)
    2.         {
    3.          
    4.             if(Input.GetTouch(0).phase==TouchPhase.Began )
    5.             {
    6.  
    7.  
    8.                     Debug.Log("Touched Screen");
    9.                
    10.  
    11.             }
    12.             if(Input.GetTouch(0).phase==TouchPhase.Moved && hit.collider.tag == "TouchLines")
    13.             {
    14.              
    15.                     Debug.Log("Touched Line containing collider");
    16.                     audiosource.Play();
    17.  
    18.             }
    19.                
    20.  
    21.         }
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    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.
     
  3. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Use PhysicsRaycaster and the modern event system. You can use IBeginDragHandler.
     
    zyonneo likes this.