Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
3
Question by Straafe · Oct 15, 2012 at 06:33 AM · inputtouchswipe

Detect the direction of a swipe? Touch screen input.

I'm trying to script a basic camera controller that pans the top-down camera as the user swipes the screen. I've almost got it working, but I have run into a weird issue. When swiping up or down, the camera reacts correctly, and moves up or down, however when swiping left or right the camera always moves ONLY right.

I've been staring at my script for hours and cannot figure out how this is possible.

Here is the snippet of my script in question (some variables are defined above this area of the script, so dont worry about stuff like that) , I coded it for left/right swipes exactly as for up/down, but it refuses to work correctly.

Any ideas?

 if (Input.touchCount == 1 && (Input.GetTouch(0).phase == TouchPhase.Began))
         {
         var touchBegin = Input.GetTouch(0);
         }
 if (Input.touchCount == 1 && (Input.GetTouch(0).phase == TouchPhase.Moved))
         {
         var touchEnd = Input.GetTouch(0);
         var difference : Vector2 = touchBegin.deltaPosition - touchEnd.deltaPosition;
             if (touchBegin.deltaPosition.x - touchEnd.deltaPosition.x > 0)
             {
                 xPos = true;
                 touchBegin = Input.GetTouch(0);
             }
             else
             {
                 xPos = false; 
                 touchBegin = Input.GetTouch(0);   
             }
             if (touchBegin.deltaPosition.y - touchEnd.deltaPosition.y > 0)
              {
                 yPos = true;
                 touchBegin = Input.GetTouch(0);
             }
             else
             {
                 yPos = false; 
                  touchBegin = Input.GetTouch(0);       
             }
             if (xPos == true)
             {
             this.transform.Translate(-difference.x*panSpeed*Time.deltaTime,0,0);   
             }
             else
             {
             this.transform.Translate(difference.x*panSpeed*Time.deltaTime,0,0);
             }
             if (yPos == true)
             {
             this.transform.Translate(0 , -difference.y*panSpeed*Time.deltaTime,0);
             }
             else
             {
             this.transform.Translate(0, difference.y*panSpeed*Time.deltaTime,0);
             }
         }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Fattie · Oct 15, 2012 at 06:41 AM

var difference : Vector2 = touchBegin.deltaPosition - touchEnd.deltaPosition;

huh? why are you using deltaPosition, champ ?

the deltaPosition IS the change, in the case of TouchPhase.Moved

in that line in your code, you'd probably just want .position - .position

So, that's probably the problem, or, one of the problems! :)

NOTE ...

these long answers may be hugely helpful to you

http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html

http://answers.unity3d.com/questions/326253/strange-touch-behavior.html#answer-326285

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 Straafe · Oct 15, 2012 at 06:46 AM 1
Share

I will try this immediately. I apologize, I am extremely new to unity and scripting in general. Thank you so much!

avatar image Straafe · Oct 15, 2012 at 08:28 AM 1
Share

Got it working thanks to you pointing me in the right direction! It still has some iffy code in there and some redundancies, but it works like a charm on my test devices!

avatar image
1

Answer by Spy-King · Nov 30, 2016 at 07:36 AM

@Straafe, This is what I came up with to detect only horizontal and vertical swipes not diagonal. I hope it helps you achieve what you want and others.

 using UnityEngine;
 
 public class SwipeInput : MonoBehaviour
 {
     public bool swiping;
 
     public float minSwipeDistance;
     public float errorRange;
 
     public SwipeDirection direction = SwipeDirection.None;
 
     public enum SwipeDirection {Right, Left, Up, Down, None}
 
     private Touch initialTouch;
 
     void Start()
     {
         Input.multiTouchEnabled = true;
     }
 
     void Update()
     {
         if (Input.touchCount <= 0)
             return;
 
         foreach (var touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 initialTouch = touch;
             }
             else if (touch.phase == TouchPhase.Moved)
             {
                 var deltaX = touch.position.x - initialTouch.position.x; //greater than 0 is right and less than zero is left
                 var deltaY = touch.position.y - initialTouch.position.y; //greater than 0 is up and less than zero is down
                 var swipeDistance = Mathf.Abs(deltaX) + Mathf.Abs(deltaY);
 
                 if (swipeDistance > minSwipeDistance && (Mathf.Abs(deltaX) > 0 || Mathf.Abs(deltaY) > 0))
                 {
                     swiping = true;
 
                     CalculateSwipeDirection(deltaX, deltaY);
                 }
             }
             else if (touch.phase == TouchPhase.Ended)
             {
                 initialTouch = new Touch();
                 swiping = false;
                 direction = SwipeDirection.None;
             }
             else if (touch.phase == TouchPhase.Canceled)
             {
                 initialTouch = new Touch();
                 swiping = false;
                 direction = SwipeDirection.None;
             }
         }
     }
 
     void CalculateSwipeDirection(float deltaX, float deltaY)
     {
         bool isHorizontalSwipe = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
 
         // horizontal swipe
         if (isHorizontalSwipe && Mathf.Abs(deltaY) <= errorRange)
         {
             //right
             if (deltaX > 0)
                 direction = SwipeDirection.Right;
             //left
             else if (deltaX < 0)
                 direction = SwipeDirection.Left;
         }
         //vertical swipe
         else if (!isHorizontalSwipe && Mathf.Abs(deltaX) <= errorRange)
         {
             //up
             if (deltaY > 0)
                 direction = SwipeDirection.Up;
             //down
             else if (deltaY < 0)
                 direction = SwipeDirection.Down;
         }
         //diagonal swipe
         else
         {
             swiping = false;
         }
     }
 }
 
Comment
Add comment · 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
0

Answer by Max_power1965 · May 10, 2017 at 12:42 PM

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.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

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

Related Questions

Joystick Zone + Screen-swipe touch input clash. Solution??? 1 Answer

Bug? Need to wait about 10 seconds to get back to normal 1 Answer

Swipe and Hold to move character . 1 Answer

Detect swipe along 3d arrows 1 Answer

mouse input to touch input help please 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges