0
\$\begingroup\$

I'm making a mobile game where you control a 3d ball by touch. So far I can get it to roll based on finger down and drag (via TouchPhase.Moved) but I want to be able to detect a fast flick or finger swipe so that I can addforce based on finger speed and let go. Bit like how I would if I was only using TouchPhase.Began and TouchPhase.Ended with calculating start/end position and time duration, but now I think I need to do something in .Moved to keep track of finger speed and get the start and end position of the beginning and end of that fast finger movement. Here's my code so far:

if (Input.touchCount > 0)
      {
          touch = Input.GetTouch(0);

          if (touch.phase == TouchPhase.Began)
          {
              touchdown = true;
          }

          if (touch.phase == TouchPhase.Moved)
          {
              if (touchdown == true)
              {
                  startPos = new Vector2(0, 0);
              }
              else {
                  startPos = Input.GetTouch(0).position;
              }
              
              touchTimeStart = Time.time;

              direction = startPos - endPos;
              timeInterval = touchTimeFinish - touchTimeStart;

              endPos = Input.GetTouch(0).position;
              touchTimeFinish = Time.time;
              
              rb.AddForce(direction.x * force, 0, direction.y * force/ moveSpeed);

              touchdown1 = false;

          } if (touch.phase == TouchPhase.Ended)
      {
          startPos = new Vector2(0, 0);
          endPos = new Vector2(0, 0); } }
\$\endgroup\$
2
  • \$\begingroup\$ It looks like the Unity Touch structure contains both deltaPosition and deltaTime variables from which you could create a velocity. How have you tried applying them to your case? \$\endgroup\$
    – DMGregory
    Jul 9 '20 at 22:58
  • \$\begingroup\$ Ok so i can get the deltaPosition which gives me a direction. I will need to get the average of deltaPosition vectors that occur in the moment the user is quickly swiping their finger. And also record the time at the start and end of that swipe for the amount of force. My question would then be how do I define the if() parameters in .Moved to notice that the finger is a swipe (quickly moving)? \$\endgroup\$
    – Brandon
    Jul 10 '20 at 1:55
0
\$\begingroup\$

I was overthinking it. Just used start and end time and position for TouchPhase.Began and .End. Also reset touch start values on TouchPhase.Stationary.

\$\endgroup\$
1
  • \$\begingroup\$ This answer would be better if it described how to use those values, with sample code that achieves your goal. \$\endgroup\$
    – DMGregory
    Jul 10 '20 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.