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.
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.