The question is answered, right answer was accepted
How to detect a swipe in ANY direction?
I would like to make a little 2D mobile game (Android) , when you swipe anywhere on the screen, the character should move in that direction. Not only left, right, up and down, but any direction.
I´m new to Unity and it would be awesome if someone could help me THX.
Answer by sunderplugs11 · Apr 03, 2016 at 04:06 AM
im not the best, but about what tou need to do is use Input.GetTouch(0).deltaposition to get your values, then setup if statements to check like (and this is probably the worst way because im new)
if(input.gettouch(0).deltaposition.x > 0){
debug.log("user is swiping to the right");
}
if(input.gettouch(0).deltaposition.x < 0){
debug.log("user is swiping to the left"):
}
if(input.gettouch(0).deltaposition.y < 0){
debug.log("user is swiping to down"):
}
if(input.gettouch(0).deltaposition.y >0){
debug.log("user is swiping to up"):
}
put all that in update.
So "Input.GetTouch" gets touches from the input manager Unity makes, then (0) signifies to use the first touch. "deltaposition" is the change in position from last frame (if i remember correctly). You just uise that data to determine how the touch is moving, which is of course a swiping motion if touch(0) has its value changed.
currently im using that for rotating a cannon up and down using
if(shootingEnabled==true && Input.touchCount > 0 && Input.GetTouch(0).deltaPosition.y > 0 )
{
RotateDown();
}
if(shootingEnabled==true && Input.touchCount > 0 && Input.GetTouch(0).deltaPosition.y < 0 )
{
MoveUP();
}
Hi guys, I wrote this simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system ins$$anonymous$$d to write long and useless code. No need to use an update or fixed update.
@$$anonymous$$ax_power1965 Can I see your code?
Follow this Question
Related Questions
Easy onTouch movement 2D 0 Answers
Error CS1026: Unexpected symbol ;, expecting ) 0 Answers
Android 2D platformer, low fps 1 Answer