Help with swiping for movement

Hi! i am new to Unity, can any please tell me what is wrong with this script, its for swiping for right,left,up,down. what i want when i swipe up it print(“up”) in log, same i want for left right.doesn’t show up in log, actually i want my character to move rite left jump and down and i have script for that. kindly help … :cry:

#pragma strict



//I use the following script, that is attached to my character (something like a chess pawn) and will move it on a board horizontally or vertically, depending on the swipes. The swipes can be made anywhere on the screen and you don't have to make them on the character.


var comfortZoneVerticalSwipe: float = 50; // the vertical swipe will have to be inside a 50 pixels horizontal boundary
var comfortZoneHorizontalSwipe: float = 50; // the horizontal swipe will have to be inside a 50 pixels vertical boundary
var minSwipeDistance: float = 14; // the swipe distance will have to be longer than this for it to be considered a swipe
//the following 4 variables are used in some cases that I don’t want my character to be allowed to move on the board (it’s a board game)
var allowGoUp: boolean = true;
var moving: boolean = true;
var allowGoRight: boolean = true;
var allowGoLeft: boolean = true;
var allowGoDown: boolean = true;
var maxSwipeTime: float= 0.5;


function Update () {
if (Input.touchCount >0) {
var touch = Input.touches[0];

switch (touch.phase) { //following are 2 cases
case TouchPhase.Began: //here begins the 1st case
var startPos = touch.position;
var startTime = Time.time;

break; //here ends the 1st case



case TouchPhase.Ended: //here begins the 2nd case
var swipeTime = Time.time - startTime;
var swipeDist = (touch.position - startPos).magnitude;
var endPos = touch.position;

if ((Mathf.Abs(touch.position.x - startPos.x))<comfortZoneVerticalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.y - startPos.y)>0 && !moving && transform.position.z<3 && allowGoUp)
{
//... then go up
moving=true;
print("up");
//[code here, to make character move the way you want (upwards)]
}


if ((Mathf.Abs(touch.position.x - startPos.x))<comfortZoneVerticalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.y - startPos.y)<0 && !moving && transform.position.z>-3 && allowGoDown)
{
//... then go down
moving=true;
//[code here, to make character move the way you want (downwards)]
}


if ((Mathf.Abs(touch.position.y - startPos.y))<comfortZoneHorizontalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.x - startPos.x)<0 && !moving && transform.position.x>-2 && allowGoLeft)
{
//... then go left
moving=true;
//[code here, to make character move the way you want (to the left)]
}

if ((Mathf.Abs(touch.position.y - startPos.y))<comfortZoneHorizontalSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDistance) && Mathf.Sign(touch.position.x - startPos.x)>0 && !moving && transform.position.x<2 && allowGoRight)
{
//...then go right
moving=true;
//[code here, to make character move the way you want (to the right)]
}
break; //here ends the 2nd case

}
}
}

Hi Omer,
What I did when I came across a situation like this, was I did the following:
I created a GUITexture to define the area I wanted the swipes in.
I then used this bit of code in a script placed on the GUITexture.

var startPos : int;
var endPos : int;
var swipeThreshold : int;

function Update(){ for (var i = 0; i
< Input.touchCount; i++){
var touch = Input.GetTouch(i);
var hold = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began &&
guiTexture.HitTest(touch.position))
{
startPos = touch.position;
}
if(touch.phase == TouchPhase.Ended &&
guiTexture.HitTest(touch.position) ||
touch.phase == TouchPhase.Canceled)
{
endPos = touch.position;
if(endPos.x > startPos.x){
if((endPos.x - startPos.x)> swipeThreshold){
//we have a right swipe
}
}
if(endPos.x < startPos.x){
if((endPos.x - startPos.x)* -1 > swipeThreshold){
//we have a left swipe
}
}
}
} }

This is what I did for left and right swipes and you can do the same for up and down by changing the the x to a y. The threshold is the minimum distance that can be counted as a swipe.
I hope this helped. Please mark it as correct if it works :smiley:
Johnny