Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by simas289 · Jul 01, 2021 at 02:05 PM · 2d2d gameswipe

2d swipe control not working.

Hey, I've been making a game and the last thing I needed to do was the swipe controls, I thought it was going to be easy because unity will have something implemented for us, but no, It is way harder than I thought so I had to follow a tutorial. what ended up happening is that my code isn't working, but I know the tutorial is good just that there is a problem in the code I wrote from the video (the video didn't have a copy of the code linked). What my code looks like now:

 private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown, isDraging;
     private Vector2 startTouch, swipeDelta;
     private float moveSpeed = 1.5f;
 
     private void Update()
     {
         tap = swipeLeft = swipeRight = swipeUp = swipeDown = isDraging = false;
 
         
 
 
         #region Standalone Input 
         //click
         if (Input.GetMouseButtonDown(0))
         {
             tap = true;
             isDraging = true;
             startTouch = Input.mousePosition;
         }
         //release
         else if (Input.GetMouseButtonUp(0))
         {
             isDraging = false;
             Reset();
         }
 
 
 
         #endregion
 
         #region Moblie Inputs
         if(Input.touches.Length > 0)
         {
             if(Input.touches[0].phase == TouchPhase.Began)
             {
                 isDraging = true;
                 tap = true;
                 startTouch = Input.touches[0].position;
             }
             else if(Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
             {
                 isDraging = false;
                 Reset();
             }
 
         }
 
 
         #endregion
 
         #region Swipe Check
         swipeDelta = Vector2.zero;
         if (isDraging)
         {
             if (Input.touches.Length > 0)
             {
                 swipeDelta = Input.touches[0].position - startTouch;
             }
             else if (Input.GetMouseButton(0))
             {
                 swipeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - startTouch;
             }
         }
         Debug.Log(swipeDelta);
         if (swipeDelta.magnitude > 125)
         {
             //which direction are we swiping in
             float x = swipeDelta.x;
             float y = swipeDelta.y;
             if(Mathf.Abs(x) > Mathf.Abs(y))
             {
                 //left or right
                 if(x < 0)
                 {
                     swipeLeft = true;
                 }
                 else
                 {
                     swipeRight = true;
                 }
 
             }
             else
             {
                 //up or down
                 if(y < 0)
                 {
                     swipeDown = true;
                 }
                 else
                 {
                     swipeUp = true;
                 }
             }
 
 
             Reset();
         }
         #endregion

the swipeDelta is always 0.0, 0.0. The startTouch is working fine and showing the position of the click. I have no idea what could be wrong here, I've checked the code a few times now, haven't found anything suspicious yet.

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

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

Answer by Devster2020 · Jul 01, 2021 at 03:03 PM

 tap = swipeLeft = swipeRight = swipeUp = swipeDown = isDraging = false;

Using this at the start of the Update make the variable isDraging every frame to false.. so your code can't works correctly because swipeDelta will be always equals to Vector2.zero!



If I can recommend a good plugin to manage gestures, I recommend you use LeanTouch. I used it in all my games.. the management is pretty easy!

Comment
Add comment · Show 4 · 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 simas289 · Jul 01, 2021 at 04:43 PM 0
Share

Oh my god thank you, It's such an easy fix and now I feel so stupid

avatar image Devster2020 simas289 · Jul 01, 2021 at 04:44 PM 0
Share

You're welcome :)

avatar image simas289 · Jul 01, 2021 at 04:46 PM 0
Share

And thank you for suggesting lean touch, as a newbie I didn't know about it, love you for all the help

avatar image Devster2020 simas289 · Jul 01, 2021 at 04:47 PM 0
Share

If you need help, just tag me :D have a nice day!

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

300 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

A problem with 2d swipe controls. 1 Answer

How to create a delay before playing an animation 1 Answer

Sprites not recognized as whole-spinning in different directions 0 Answers

Prefab to a tile script 0 Answers

Huge lag when toggling between Tile maps in one scene. 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