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 …
#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
}
}
}