Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 abhijeet1001 · Dec 13, 2016 at 06:20 PM · c#inputtouchswipehold

Swipe and Hold to move character .

Hi all ,

I am trying to move my character by following gesture : User swipes on screen ( this swipe determines which direction character is suppose to move ) and then without lifting the finger holds it down on the screen . When user holds it down i want it to keep looping (calling the move command with specific direction ) until user takes his finger off the screen . I tried using touchphase.Stationary instead of touchphase.ended which i am using in my current script , the results were wonky and weird and not perfect and i am clueless as to where to start. I hope somebody from here will b able to help me out .

 using UnityEngine;
 using System.Collections;
 
 public class SwipeDetector : MonoBehaviour 
 {
     private float fingerStartTime  = 0.0f;
     private Vector2 fingerStartPos = Vector2.zero;
 
     private bool isSwipe = false;
     private float minSwipeDist  = 30.0f;
     private float maxSwipeTime = 0.5f;
 
     public GameObject player;
     public PlayerMovement playerMovementScript;

     void Start(){
 
         
 
     }
 
     void Update()
     {
 
 #if UNITY_EDITOR 
 
         if (Input.GetKeyDown (KeyCode.A)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.left = true;
             }
             
 
         }else if (Input.GetKeyDown (KeyCode.S)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.down = true;
             }
         
 
         }else if (Input.GetKeyDown (KeyCode.D)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.right = true;
             }
             
 
         }else if (Input.GetKeyDown (KeyCode.W)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.top = true;
             }
             
 
         }
 
 #else
 
         if (Input.touchCount > 0 && Time.timeScale > 0.0f) 
         {
 
             foreach (Touch touch in Input.touches) 
             {
 
                 switch (touch.phase) 
                 {
 
                 case TouchPhase.Began:
                     /* this is a new touch */
                     isSwipe = true;
                     fingerStartTime = Time.time;
                     fingerStartPos = touch.position;
                     break;
 
                 case TouchPhase.Canceled:
                     /* The touch is being canceled */
                     isSwipe = false;
                     break;
 
                 case TouchPhase.Ende:
 
                     float gestureTime = Time.time - fingerStartTime;
                     float gestureDist = (touch.position - fingerStartPos).magnitude;
 
                     if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist) {
 
                         Vector2 direction = touch.position - fingerStartPos;
                         //Vector2 swipeType = Vector2.zero;
                         int swipeType = -1;
     //                        if (Mathf.Abs(direction.normalized.x) > 0.9)
     //                        {
     //
     //                            if (Mathf.Sign(direction.x) > 0) swipeType = 0; // swipe right
     //                            else swipeType = 1; // swipe left
     //
     //                        }
     //                        else if (Mathf.Abs(direction.normalized.y) > 0.9)
     //                        {
     //                            if (Mathf.Sign(direction.y) > 0) swipeType = 2; // swipe up
     //                            else swipeType = 3; // swipe down
     //                        }
     //                        else
     //                        {
                         // diagonal:
                         if (Mathf.Sign (direction.x) > 0) {
 
                             if (Mathf.Sign (direction.y) > 0)
                                 swipeType = 4; // swipe diagonal up-right
                             else
                                 swipeType = 5; // swipe diagonal down-right
 
                         } else {
 
                             if (Mathf.Sign (direction.y) > 0)
                                 swipeType = 6; // swipe diagonal up-left
                             else
                                 swipeType = 7; // swipe diagonal down-left
 
                         }
 
                         //                        }
 
                         switch (swipeType) {
 
                         case 0: //right
     //                            swipeDirection.GetComponent<Text>().text = "right";
                             break;
 
 
                         case 1: //left
     //                            swipeDirection.GetComponent<Text>().text = "left";
                             break;
 
                         case 2: //up
     //                            swipeDirection.GetComponent<Text>().text = "up";
                             break;
 
                         case 3: //down
     //                            swipeDirection.GetComponent<Text> ().text = "down";
                             break;
 
                         case 4: //up right
 //                            swipeDirection.GetComponent<Text> ().text = "upright";
                             playerMovementScript.right = true;
                             break;
                         case 5: //down right
 //                            swipeDirection.GetComponent<Text>().text = "downright";
                             playerMovementScript.down = true;
                             break;
 
                         case 6: //up left
 //                            swipeDirection.GetComponent<Text>().text = "upleft";
                             playerMovementScript.top = true;
                             break;
 
                         case 7: //down left
 //                            swipeDirection.GetComponent<Text>().text = "downleft";
                             playerMovementScript.left = true;
                             break;
 
                         }
 
                     }
 
                     break;
 
                 }
 
             }
 
         }
         #endif
     }
 
 }







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
Best Answer

Answer by abhijeet1001 · Dec 13, 2016 at 10:41 PM

Sometimes all you need is some alone time and brain power to find the solution which in my case was pretty easy . Here is the final script :

 using UnityEngine;
 using System.Collections;
 
 public class SwipeDetector : MonoBehaviour 
 {
     private float fingerStartTime  = 0.0f;
     private Vector2 fingerStartPos = Vector2.zero;
 
     private bool isSwipe = false;
     private float minSwipeDist  = 30.0f;
     private float maxSwipeTime = 10f;
 
     public GameObject player;
     public PlayerMovement playerMovementScript;
 
     bool canInvoke = true;
 
 
     void Start(){
 
 
     }
 
     void Update()
     {
 
 #if UNITY_EDITOR 
 
         if (Input.GetKeyDown (KeyCode.A)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.left = true;
             }
             
         }else if (Input.GetKeyDown (KeyCode.S)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.down = true;
             }
             
         }else if (Input.GetKeyDown (KeyCode.D)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.right = true;
             }
             
         }else if (Input.GetKeyDown (KeyCode.W)) {
             if(playerMovementScript.enabled == true){
                 playerMovementScript.top = true;
             }
             
         }
 
 #else
 
         if (Input.touchCount > 0 && Time.timeScale > 0.0f) 
         {
 
             foreach (Touch touch in Input.touches) 
             {
                 if (touch.phase == TouchPhase.Began){
                     isSwipe = true;
                     fingerStartTime = Time.time;
                     fingerStartPos = touch.position;
                 }
 
                 if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
 
 
                     float gestureTime = Time.time - fingerStartTime;
                     float gestureDist = (touch.position - fingerStartPos).magnitude;
 
                     if (canInvoke && isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist) {
 
                         canInvoke = false;
                         Invoke ("invokeMovement", .4f);
 
                         Vector2 direction = touch.position - fingerStartPos;
                         //Vector2 swipeType = Vector2.zero;
                         int swipeType = -1;
                         //                        if (Mathf.Abs(direction.normalized.x) > 0.9)
                         //                        {
                         //
                         //                            if (Mathf.Sign(direction.x) > 0) swipeType = 0; // swipe right
                         //                            else swipeType = 1; // swipe left
                         //
                         //                        }
                         //                        else if (Mathf.Abs(direction.normalized.y) > 0.9)
                         //                        {
                         //                            if (Mathf.Sign(direction.y) > 0) swipeType = 2; // swipe up
                         //                            else swipeType = 3; // swipe down
                         //                        }
                         //                        else
                         //                        {
                         // diagonal:
                         if (Mathf.Sign (direction.x) > 0) {
 
                             if (Mathf.Sign (direction.y) > 0)
                                 swipeType = 4; // swipe diagonal up-right
                     else
                                 swipeType = 5; // swipe diagonal down-right
 
                         } else {
 
                             if (Mathf.Sign (direction.y) > 0)
                                 swipeType = 6; // swipe diagonal up-left
                     else
                                 swipeType = 7; // swipe diagonal down-left
 
                         }
 
                         //                        }
 
                         switch (swipeType) {
 
                         case 0: //right
 //                            swipeDirection.GetComponent<Text>().text = "right";
                             break;
 
 
                         case 1: //left
 //                            swipeDirection.GetComponent<Text>().text = "left";
                             break;
 
                         case 2: //up
 //                            swipeDirection.GetComponent<Text>().text = "up";
                             break;
 
                         case 3: //down
 //                            swipeDirection.GetComponent<Text> ().text = "down";
                             break;
 
                         case 4: //up right
 //                            swipeDirection.GetComponent<Text> ().text = "upright";
                             playerMovementScript.right = true;
                             break;
                         case 5: //down right
 //                            swipeDirection.GetComponent<Text>().text = "downright";
                             playerMovementScript.down = true;
                             break;
 
                         case 6: //up left
 //                            swipeDirection.GetComponent<Text>().text = "upleft";
                             playerMovementScript.top = true;
                             break;
 
                         case 7: //down left
 //                            swipeDirection.GetComponent<Text>().text = "downleft";
                             playerMovementScript.left = true;
                             break;
 
                         }
 
                     }
 
                 } else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
                     canInvoke = true;
                 }
                 
             }
 
         }
         #endif
     }
 
     public void invokeMovement(){
 
         canInvoke = true;
 
     }
 }

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

88 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

Related Questions

Input.GetTouch(0).position.x and TouchPhase.Began 1 Answer

Swipe on the part of the screen 1 Answer

How to convert Touch Input To Mouse Input C# unity? 1 Answer

codes for detecting mobile double tap and hold 1 Answer

C# Screen.width Touch Input In Multiple Scripts Affect Each Other - Please Help 1 Answer


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