# Detect the swipe and add force respective to it?

Hello,

I am trying to detect a swipe on the iPad and apply a force in its concerned direction and length(magnitude). So I just take the input of the swipes that are linear and then use them as the input. I, however did script but I did not bring in the desired.

CODE

``````if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
final = Vector3.zero;
length = 0;
SW = false;
Vector2 touchDeltaPosition = Input.GetTouch(0).position;
startpos = new Vector3(touchDeltaPosition.x,0,touchDeltaPosition.y);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
SW = true;
}

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled)
{
SW = false;
}

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
if(SW)
{
Vector2 touchPosition = Input.GetTouch(0).position;
endPos = new Vector3(touchPosition.x,0,touchPosition.y);
final = endpos - startpos;
length = final.magnitude;
}
}
``````

Can you guys help me on this issue?

THANK YOU

I created a new project using your code, and everything seemed to work properly. You did have one syntax error (typo on “endPos”, which should be “endpos”), but other than that everything looked fine.

I simply created an empty project with the following script:

``````using UnityEngine;
using System.Collections;

public class pooh : MonoBehaviour
{

// Use this for initialization
void Start ()
{

}

private float length = 0;
private bool SW = false;
private Vector3 final;
private Vector3 startpos;
private Vector3 endpos;

// Update is called once per frame
void Update ()
{

if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
final = Vector3.zero;
length = 0;
SW = false;
Vector2 touchDeltaPosition = Input.GetTouch (0).position;
startpos = new Vector3 (touchDeltaPosition.x, 0, touchDeltaPosition.y);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved)
{
SW = true;
}

if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Canceled)
{
SW = false;
}

if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended)
{
if (SW)
{
Vector2 touchPosition = Input.GetTouch (0).position;
endpos = new Vector3 (touchPosition.x, 0, touchPosition.y);
final = endpos - startpos;
length = final.magnitude;
}
}
}

void OnGUI()
{
GUI.Box (new Rect (50,300,500,30), "length: " + length);
}
}
``````

The script is called “pooh.cs”. Don’t worry, it’s not dirty. This is like Winnie the Pooh.

Anyway, I then attached the pooh script to the “Main Camera” object. Everything works like you’ve coded it, with the “Length” display updating after each swipe.

If the steps I’ve included don’t help you, maybe you can explain the exact bug you’re seeing.

-Kevin

using UnityEngine;
using System.Collections;

``````public class SwipeDetector : MonoBehaviour
{

public float minSwipeDistY;

public float minSwipeDistX;

private Vector2 startPos;

void Update()
{
//#if UNITY_ANDROID
if (Input.touchCount > 0)

{

Touch touch = Input.touches[0];

switch (touch.phase)

{

case TouchPhase.Began:

startPos = touch.position;

break;

case TouchPhase.Ended:

float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;

if (swipeDistVertical > minSwipeDistY)

{

float swipeValue = Mathf.Sign(touch.position.y - startPos.y);

if (swipeValue > 0)//up swipe

//Jump ();

else if (swipeValue < 0)//down swipe

//Shrink ();

}

float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;

if (swipeDistHorizontal > minSwipeDistX)

{

float swipeValue = Mathf.Sign(touch.position.x - startPos.x);

if (swipeValue > 0)//right swipe

//MoveRight ();

else if (swipeValue < 0)//left swipe

//MoveLeft ();

}
break;
}
}
}
}
``````

This post save my project: http://pfonseca.com/swipe-detection-on-unity/