D
D
DuckMcQuack2019-02-07 19:34:44
C++ / C#
DuckMcQuack, 2019-02-07 19:34:44

How do you implement swipes in unity?

I tried in many ways, and universal for PC and Android through MousePosition, iDragHandler, touch.deltaPosition and separately counting the difference between positions. The variant with touch.deltaPosition seemed the most correct

void Update () {
    
    Touch touch = Input.GetTouch(0);
    if(touch.deltaPosition.x>0f){
      Debug.Log ("right");
    }
    if(touch.deltaPosition.x<0f){
      Debug.Log ("left");
    }
      
  }

but he does a lot in one swipe

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2019-02-07
@DuckMcQuack

if you all did this, then it’s not surprising ..
the general principle of the “swipe” gesture
is to remember the starting position (click / touch it doesn’t matter here)) plus maybe a timer and a finger / touch index if you need a multi-touch
- count the offset until you release it
- at the moment you release it - calculate the offset. (it is possible to check the time so that it does not count for a swipe if "led to the side for five seconds")
- the calculated offset. depending on the direction, count for the desired "swipe"
and not every frame as you count))
The wheelbarrow has phases. start. motion. cancellation. end... see the documentation.
as the mouse actually has - PRESSED. CLAMPED. RELEASED ))
https://docs.unity3d.com/ScriptReference/Touch-pha...
https://docs.unity3d.com/ScriptReference/Input.Get...
and so on

M
mopsicus, 2019-02-14
@mopsicus

I recommend the free Lean Touch asset: https://assetstore.unity.com/packages/tools/input-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question