Minimum Drag Distance

Hi All,

I’m trying to set a minimum drag distance for the user controls. Essentially I need this in order to differentiate between a drag from a tap. I’ve tried a method that I used in another game, but couldn’t get it to work.

Here is the the basis of my drag script:

public static event Action<Vector3> OnSwipeStarted;
        public static event Action<Vector3> OnSwipe;
        public static event Action<Vector3> OnSwipeEnded;

void Update()
        {
            //set minimum drag distance
            //Calculate drag distance
            //if (drag distance > minimum drag distance)
            //This is a drag


            if (Input.GetMouseButtonDown (0))
            {
                startPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                if (OnSwipeStarted != null)
                    OnSwipeStarted (startPosition);
            }
            else if (Input.GetMouseButton (0))
            {
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                Vector3 movement = startPosition - currentPosition;

                if (OnSwipe != null)
                    OnSwipe (movement);

            }
            else if (Input.GetMouseButtonUp(0))
            {
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                Vector3 movement = startPosition - currentPosition;

                if (OnSwipeEnded != null)
                    OnSwipeEnded (movement);
             }
        }

Here is the concept that I’m trying to include in the above script:

{
         if(Input.GetMouseButtonDown(0))
         {
             mouseDragStart = Input.mousePosition;
         }

         if(Input.GetMouseButtonUp(0))
         {
             mouseDragEnd = Input.mousePosition;
             mouseDistance =  Mathf.Sqrt((mouseDragEnd.x * mouseDragStart.x) + (mouseDragEnd.y * mouseDragStart.y));
         }

    if (mouseDistance > 50)
    {
           //This is a swipe
    }

Im probably overthinking this but I haven’t been able to successfully “weave” these two together! :face_with_spiral_eyes:
Thanks in advance!

Is this for a 2D or 3D game?

3D but Im only using X and Y

If you Z position is going to be contant, then you could just do this;

using UnityEngine;

public class MouseDragDetect : MonoBehaviour
{
    public float DistanceForSwipe = 2.5f;

    private bool _wasSwipe = false;
    private Vector3 _startPos;

    // Update is called once per frame
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            _startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
           Debug.Log("Mouse Start Position : " + _startPos);
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Vector3 _endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            float distance = Vector3.Distance(_startPos, _endPos);

            _wasSwipe = distance >= DistanceForSwipe;

            Debug.Log("Mouse End Position : " + _endPos);
            Debug.Log("Mouse Distance : " + distance);
            Debug.Log("Was Swipe : " + _wasSwipe);
        }
    }
}
1 Like

Thanks for the reply!

So I got it working (mostly) with a few tweaks. I’m quite new to programming and game dev so I’m not exactly sure what I did to get things working :eyes:. The only thing that is not working is the “distanceForSwipe” variable. It seems to be somehow stuck on 2.5f. If I change it to something like 1, the “wasSwipe” still registers as false if the distance was < 2.5.

void Update()
        {
            if (Input.GetMouseButtonDown (0))
            {
                startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Debug.Log("Mouse Start Position : " + startPos);


                startPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                if (OnSwipeStarted != null)
                    OnSwipeStarted (startPosition);

            }

            else if (Input.GetMouseButton (0))
            {
                Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                Vector3 movement = startPosition - currentPosition;

                if (OnSwipe != null)
                    OnSwipe (movement);

            }
            else if (Input.GetMouseButtonUp(0))
            {
                Vector3 endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                float distance = Vector3.Distance(startPos, endPos);

                wasSwipe = distance >= distanceForSwipe;

                Debug.Log("Mouse End Position : " + endPos);
                Debug.Log("Mouse Distance : " + distance);
                Debug.Log("Was Swipe : " + wasSwipe);

                Vector3 currentPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                Vector3 movement = startPosition - currentPosition;
                if (wasSwipe == true)
                {
                    if (OnSwipeEnded != null)
                        OnSwipeEnded (movement);
                }
            }
        }

This “brick breaker” type game is similar to what I am making.

change the value of wasSwipe in the inspector, not in the code. If you want to change it only in code, then make it private otherwise Unity will be overriding the value at the moment.

Also, you don’t need most of your original code now. Not sure why you are using the events or where they also being used in your game.

1 Like

Ahhhh, well that was a silly mistake :sweat_smile:…

I think the events are being used in another script (There is a function in another script that is used for “firing” that uses them). When I use your code, it wont fire so I’m assuming I need those events. I haven’t written most of this, just got it off of the asset store to learn from. Slowly picking apart everything and adding things here and there to see what I can learn from it :slight_smile:

Thanks again for your help!