Collision detection with swipe gesture

If this question has already been answered please point me in the correct direction. Basically the topic says my problem I used the simple example in unity scripting reference to get the swipe working for left or right but I don’t know how to do the collision detection when my finger hits a certain object, I did see some answers about raycasting which didn’t make sense because I am not using an object todo the swipe I have my swipe script on a camera and even I did do an object it would not have been moving, so just need a little help and direction.

Thanks

Alright so I think that I may have this figured out but not for sure just yet here is my code with raycast believe it or not;

if (Input.touchCount > 0  Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            RaycastHit hit;
            Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
            var ray = Camera.main.ScreenPointToRay(touchDeltaPosition);
          if (touchDeltaPosition.x > 5)
                text.text = "Right Swipe";
          if (touchDeltaPosition.x < 5)
                text.text = "Left Swipe";
          if (Physics.Raycast(ray, out hit, 2500))
          {
             if(hit.transform.tag == "Item") text.text = "Hit";
          }
            

        }

If anyone has anything else to add feel free.

Alright so I am guessing my line for ray is not correct because I have a touchdeltaposition as the vector 3 point from the camera, so I need a little help in trying to see how to detect the finger swiping the item instead of just somewhere on the screen.

After searching I found some code and combined with mine and it works perfectly, the trick to getting it work after you have written code is that you don’t have press hard on the screen just barely touch it and it work 100% of the time. :stuck_out_tongue: Silly me.

 switch (touch.phase)
            { 

                case TouchPhase.Began:
                    startPos = touch.position;
                    startTime = Time.time;
                   
                    break; //here ends the 1st case

                case TouchPhase.Moved: 
                    float swipeTime = Time.time - startTime;
                    float swipeDist = (touch.position - startPos).magnitude;
                    var ray = Camera.main.ScreenPointToRay(touch.position);
                   
                      
                    if ((Mathf.Abs(touch.position.x - startPos.x)) < comfortZoneVerticalSwipe  (swipeTime < maxSwipeTime)  (swipeDist > minSwipeDistance)  Mathf.Sign(touch.position.y - startPos.y) > 0  Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
                        {
                            if (hit.transform.gameObject.tag == whatever you want)
                            {
                               TEXT.TEXT = "Up Collide";
                            }
                            
                        }

                    if ((Mathf.Abs(touch.position.x - startPos.x)) < comfortZoneVerticalSwipe  (swipeTime < maxSwipeTime)  (swipeDist > minSwipeDistance)  Mathf.Sign(touch.position.y - startPos.y) > 0  !Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
                    {
                        
                            text.text = "Up";

                    }
if ((Mathf.Abs(touch.position.x - startPos.x)) < comfortZoneVerticalSwipe  (swipeTime < maxSwipeTime)  (swipeDist > minSwipeDistance)  Mathf.Sign(touch.position.y - startPos.y) < 0  !Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
                    {
                        text.text = "Down";

                    }

You get the point but it is here if anyone has trouble I got the main portion of my code from here:
http://forum.unity3d.com/threads/48601-Swipe-help-please/page2?highlight=swipe

andeeeee wrote the code the other members modified it so I added what I had to it for the collision of the object portion.

1 Like

Any updated code using?

  if(Input.GetTouch(0).phase==TouchPhase.Moved)
{
//Code to detect the collider with tag
}