-1

I am new to Unity and I am trying to make a swipe input for mobile using the Input.touch[0]. I am getting the point touched and I'm using it to calculate the swipe delta, but the problem is the input coordinates are from the left corner of the screen. I need 0,0 from the center of the screen. This is my script:

    public class SwipeController : MonoBehaviour
    {
    private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    private bool isDragging;
    private Vector2 startTouch, swipeDelta;

    private void Update()
        {
        tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

    //stand Alone Inputs

    if (Input.GetMouseButtonDown(0))
    {
        isDragging = true;
        tap = true;
        startTouch = Input.mousePosition;
    } else if (Input.GetMouseButtonUp(0)) {
        isDragging = false;
        Reset();
    }


    //MOBILE INPUTS

    if (Input.touches.Length > 0 )
    {
        if(Input.touches[0].phase == TouchPhase.Began)
        {
            tap = true;
            isDragging = true;
            startTouch = Input.touches[0].position;
        } else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            isDragging = false;
            Reset();
        }
    }

    //Distance calcs

    if (isDragging)
    {
        if(Input.touches.Length > 0)
        {
            swipeDelta = Input.touches[0].position - startTouch;
        } else if (Input.GetMouseButton(0)) {
            swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }

        //DEAD ZONE?

    if (swipeDelta.magnitude > 100)
        {
            float x = swipeDelta.x;
            float y = swipeDelta.y;

            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                //left or right
                if (x < 0)
                {
                    swipeLeft = true;
                } else
                {
                    swipeRight = true;
                }
            } else
            {
                // top or bottom
                if (y < 0 )
                {
                    swipeDown = true;
                } else
                {
                    swipeUp = true;
                }
            }

            Reset();
        }
    }
}

    private void Reset()
    {
       startTouch = swipeDelta = Vector2.zero;
    }

    public Vector2 SwipeDelta { get { return swipeDelta; } }
    public bool SwipeLeft { get { return swipeLeft; } }
    public bool SwipeRight { get { return swipeRight; } }
    public bool SwipeUp { get { return swipeUp; } }
    public bool SwipeDown { get { return swipeDown; } }
    }

What am I missing?

Daniel Marín
  • 1,369
  • 14
  • 36
The Keeper
  • 429
  • 7
  • 16
  • If all you care about is the delta, then it shouldn't matter what part of the screen is the origin. Can you be more descriptive about what the current behaviour is vs the expected behaviour? – Ruzihm Sep 16 '19 at 15:54
  • 1
    Possible duplicate of [Detect swipe gesture direction](https://stackoverflow.com/questions/41491765/detect-swipe-gesture-direction) – Ruzihm Sep 16 '19 at 16:02
  • Offset by half the width and half the height of the screen resolution? – Draco18s no longer trusts SE Sep 16 '19 at 20:22

1 Answers1

0

You are looking for Camera.ScreenToWorldPoint.

Touch input is recorded in pixels, not world units. If you feed the touch position into your camera's ScreenToWorldPoint method, the returning Vector3 is the world position that was touched. Typically users will just use:

Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));

but it is advised to cache the Camera.main reference to improve performance. A good example of this is in official documentation linked above.

Erik Overflow
  • 2,220
  • 1
  • 5
  • 16
  • If the camera is in perspective mode, this will always return the position of the camera and have nothing to do with the position of touches/mouse. Even if the camera is in orthographic mode, this will return a projection of the posiition of the touch/mouse in world space, not screen space like asker wants. – Ruzihm Sep 16 '19 at 16:05
  • I assumed that the asker was not familiar with Unity as an engine, and not the mathematics behind vectors. The reason I assumed this was because finding 0,0-centered in screen space provides no value (as you mentioned earlier, the delta doesn't change). There were quite a few grammar mistakes that lead me to derive this answer. With those mistakes appearing to have been corrected, you are correct. – Erik Overflow Sep 16 '19 at 16:17