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 02, 2021 at 10:21 AM · 2d2d gameswipe

A problem with 2d swipe controls.

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
0

Answer by Llama_w_2Ls · Jul 02, 2021 at 12:09 PM

I followed that tutorial a long time ago. In my opinion, it's overly complicated. Swipe controls shouldn't have to look this daunting.


The first thing you're going to want to do, is find out the direction in which you swiped. You can do this in the Update method, using the mouse position when you first clicked, and the mouse position when you let go of the mouse as your direction. For example:

 Vector3 StartPos;
 
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         StartPos = Input.mousePosition;
     }
 
     if (Input.GetMouseButtonUp(0))
     {
         var swipeDirection = Input.mousePosition - StartPos;
 
         // Do something with swipeDirection...
     }
 }



Finally, you need to pass in the raw direction into a method, that processes the direction and outputs a swipe direction value. For example:

 void GetSwipeDirection(Vector3 direction)
 {
     // If the swipe was unnoticable (accidental touch) don't move
     if (direction.magnitude < DeadZone)
         return;
 
     var normalizedDir = direction.normalized;
 
     // If the swipe was diagonal, return the closest straight
     // direction (so North-North-East = North)
     normalizedDir = Mathf.Abs(normalizedDir.x) > Mathf.Abs(normalizedDir.y)
         ? new Vector3(normalizedDir.x, 0)
         : new Vector3(0, normalizedDir.y);
 
     // Round values to nearest 1
     var adjustedDir = new Vector2Int
     (
         Mathf.RoundToInt(normalizedDir.x),
         Mathf.RoundToInt(normalizedDir.y)
     );
 
     // Player movement function (moves the player in that direction)
     player.Move(adjustedDir);
 }



Full Script

 public class SwipeControls : MonoBehaviour
 {
     public Player player;
     public float DeadZone;
 
     Vector3 StartPos;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             StartPos = Input.mousePosition;
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             var swipeDirection = Input.mousePosition - StartPos;
             GetSwipeDirection(swipeDirection);
         }
     }
 
     void GetSwipeDirection(Vector3 direction)
     {
         // If the swipe was unnoticable (accidental touch) don't move
         if (direction.magnitude < DeadZone)
             return;
 
         var normalizedDir = direction.normalized;
 
         // If the swipe was diagonal, return the closest straight
         // direction (so North-North-East = North)
         normalizedDir = Mathf.Abs(normalizedDir.x) > Mathf.Abs(normalizedDir.y)
             ? new Vector3(normalizedDir.x, 0)
             : new Vector3(0, normalizedDir.y);
 
         // Round values to nearest 1
         var adjustedDir = new Vector2Int
         (
             Mathf.RoundToInt(normalizedDir.x),
             Mathf.RoundToInt(normalizedDir.y)
         );
 
         // Player movement function (moves the player in that direction)
         player.Move(adjustedDir);
     }
 }


@simas289

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

299 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

Related Questions

2d swipe control not working. 1 Answer

Random spawn outside viewport - Wrap around screen 2 Answers

How to make layers become visible with a button prompt?,How to make entire layers active/inactive with a button press 0 Answers

Snake game doesn't respond to input 0 Answers

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 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