- Home /
Swipe movement help
I know questions regarding swipe movement are very common. But I assure you I have been through over 10 threads, multiple tutorials and what not and am unable to answer this question.
I have a player that moves around the gameboard, by a fixed distance, depending on which direction you swipe in.
Here is the current swipe script I'm using
void touchcheck()
{
if(Input.touchCount == 1)
{
//its always x=-1 and y=-1 if touchCount == 0 Resets
if(deltaPosition.x == -1 && deltaPosition.y == -1)
deltaPosition = Input.GetTouch(0).position; //First touch point stored
afterDeltaPosition = Input.GetTouch(0).position;//the swiping points
angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ; // angle in degrees from 0,-180 and 0,180)
Debug.Log("Angle "+ angle);
//right swipe
if(angle < 45f && angle > -45f && !restrictR)
{
if(Physics.Raycast(transform.position,Vector3.right,out righthitter))
{
restrictR = true;
right = true;
R = righthitter.distance;
RightObj = righthitter.collider.gameObject;
if(RightObj.tag=="interactable_tile")
{
R = R +1f;
}
if(R >= 1f)
MoveRight();
}
}
//front swipe
else if(angle < 135f && angle > 45f && !restrictF)
{
restrictF = true;
forward = true;
if(Physics.Raycast(transform.position,Vector3.forward,out fronthitter))
{
F=fronthitter.distance;
FrontObj = fronthitter.collider.gameObject;
if(FrontObj.tag=="interactable_tile")
{
F=F+1f;
}
if(F >= 1f)
MoveFront();
}
}
//left swipe
else if(angle > 135f && angle < -135f && !restrictL)
{
if(Physics.Raycast(transform.position,Vector3.left,out lefthitter))
{
restrictL = true;
left = true;
L = lefthitter.distance;
LeftObj = lefthitter.collider.gameObject;
if(LeftObj.tag=="interactable_tile")
{
L = L +1f;
}
if(L >= 1f)
MoveLeft();
}
}
//back swipe
else if(angle > -135f && angle < -45f && !restrictB)
{
if(Physics.Raycast(transform.position,-Vector3.forward,out backhitter))
{
restrictB = true;
backward = true;
B=backhitter.distance;
BackObj = backhitter.collider.gameObject;
if(BackObj.tag=="interactable_tile")
{
B=B+1f;
}
if(B >= 1f)
MoveBack();
}
}
}
else if( Input.touchCount == 0)
{
if(deltaPosition.x != -1 && deltaPosition.y != -1)
{
deltaPosition.x=-1;
deltaPosition.y=-1;
}
}
}
The problem with this is, The left swipe DOES NOT WORK. If you swipe to the left, it does the same as a right swipe.
I want my swipe to end when I move my finger a certain distance across the screen. Currently what's happening is, I can do 2 swipes really fast, and confuse my game to move the player in a weird fashion.
Also, is there a way to get the user to remove his finger before he can start another swipe?
Answer by tanoshimi · Sep 11, 2013 at 03:53 PM
//left swipe
else if(angle > 135f && angle < -135f && !restrictL)
No value of angle can ever be greater than 135 and less than -135. Did you mean || instead of && ?
Why not? 135 to 180, and 180 to -135. Anyways, fixed it by using 215 degrees ins$$anonymous$$d of -135.
The bigger problem that still is unfixed is the restriction of axis of movement
You've missed the point: give me a specific value of angle that satisifies the condition of being greater than 135 and being less than -135...
135 to -135 indicates the angle from 135 to 225 degrees. It's just a nomenclature.
Anyways my current script isn't detecting swipes properly, so please help me out on that rather than debating on the angles.
I'm trying to help you, and you're not listening.... the reason why your script is not detecting left swipes is because your entire //left swipe
block of code begins with this test:
else if(angle > 135f && angle < -135f && !restrictL)
This will NEVER BE TRUE, BECAUSE YOU CANNOT HAVE A NU$$anonymous$$BER THAT IS GREATER THAN 135 AND LESS THAN -135. That's why your left swipe isn't working.
Boss, I already fixed that. Which is what I'm trying to tell you
Your answer
Follow this Question
Related Questions
iPhone - How to calculate a decent touch swipe speed? 2 Answers
Recommendations on best way to begin implementing Swipe for iOS game? 1 Answer
Swipe Drag Select Gameobjects :( ? 1 Answer
How to move 2D object on touch in Unity 4.3 1 Answer
conditional gameobject spawning from collision array help please? 1 Answer