• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 03, 2016 at 02:33 PM by RealJo for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RealJo · Apr 02, 2016 at 10:39 PM · android2dtouch controlsswipe

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1

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();
         }
 


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Max_power1965 · May 10, 2017 at 12:37 PM 1
Share

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 instead to write long and useless code. No need to use an update or fixed update.

avatar image jamiecorkhill5 Max_power1965 · Feb 17, 2018 at 09:03 PM 0
Share

@Max_power1965 Can I see your code?

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error CS1026: Unexpected symbol ;, expecting ) 0 Answers

Easy onTouch movement 2D 0 Answers

How to drag object in GridLayoutGroup 0 Answers

Objects jump/warp for a little distance after regain the focus in Android 0 Answers

Android gradle build failing after update to 2018.1 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges