- Home /
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 .. :'(
#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
}
}
}
Answer by sillanstudios · Jan 02, 2014 at 05:14 PM
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 :D Johnny
@Johnny thanks i applied the code of my own which is quite related with your code :p . it shows right and left in log but my character doesn't move to the right position neither left. i have applied position change through lerp , i want to move it in exact right left positions and for middle as well. position change work in seperate script code but when i apply in this script it doesn't work. i don't know what's the problem. #pragma strict
var position$$anonymous$$ : Vector3 = new Vector3(39.90576,1.292813,1.095224);
var positionR : Vector3 = new Vector3(39.90576,1.292813,7.095224);
var positionL : Vector3 = new Vector3(39.90576,1.292813,-3.095224);
var startTouch : Vector2;
public var smooth : float;
private var newPosition : Vector3;
function Awake ()
{
newPosition = transform.position;
}
function Update ()
{
//PositionChanging();
if (Input.touchCount > 0)
{
//print("start");
var touch : Touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
//print("began"+newPosition);
startTouch = touch.position;
// print("start position is: " +touch.position);
}
if(touch.phase == TouchPhase.$$anonymous$$oved)
{
// print("moved");
}
if(touch.phase == TouchPhase.Ended)
{
/////////////////////for right move////////////////
if(startTouch.x-100 > touch.position.x)
{
//Right();
//newPosition = transform.position;
newPosition = positionL;
print("left agya");
}
/////////////////////for left move////////////////
if(startTouch.x+100 < touch.position.x)
{
print("right gya");
newPosition = positionR;
}
}
}
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
and by mistake i have written right for left move and left for right move :p ... but code works for right and left correctly