• 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
Question by jckmull86 · Mar 02, 2018 at 10:00 PM · touchswipetouchphasetouchesphase

Swipe horizontally and hold finger down to move character?

Hi!

Working on my first project, and I hit a wall with my character controller. The game is an ios Mobile 2D side scroller.

The mechanic has a few parameters.

If the player swipes left to right, and keeps finger down, and the swipe speed is slow enough, the character walks. But if the swipe speed is fast enough, they run. If the player swipes back to the left, with all the same parameters, the character walks or runs to the left depending on swipe speed.

Below is as far as I could get with my limited scripting knowledge and online research. I succeeded in moving the character left and right with the swipe but here are my problems. - Character shakes as it moves. - The finger stationary statement calls the run speed no matter if the finger arrived there on a slow swiping speed.

You can drop the script on an object and observe what I'm seeing. Thank you for any help, and if you have time, please explain what you're doing to make it work. I really want to get better at this.

 public class secondTest : MonoBehaviour 
 {
     private Vector2 fingerDown;
     private Vector2 fingerUp;
 
     public float walk = 2f;
     public float run = 10f;
     public float walkRunThreshold = 5f;
 
 
 
     // Update is called once per frame
     void Update()
     {
 
         foreach (Touch touch in Input.touches)
         {
             float swipeSpeed = touch.deltaPosition.magnitude / touch.deltaTime;
 
             if (touch.phase == TouchPhase.Began)
             {
                 fingerUp = touch.position;
                 fingerDown = touch.position;
             }
 
             //Detects Swipe while finger is still moving
             if (touch.phase == TouchPhase.Moved && swipeSpeed >= walkRunThreshold) 
             {
                 fingerDown = touch.position;
 
                 if (fingerDown.x - fingerUp.x > 0) 
                 {
                     WalkRight ();
                 } 
                 else if (fingerDown.x - fingerUp.x < 0) 
                 {
                     WalkLeft ();
                 }
             }
 
             else if (touch.phase == TouchPhase.Moved && swipeSpeed < walkRunThreshold)
             {
                 fingerDown = touch.position;
 
                 if (fingerDown.x - fingerUp.x > 0)//Right swipe
                 {
                     RunRight();
                 }
                 else if (fingerDown.x - fingerUp.x < 0)//Left swipe
                 {
                     RunLeft();
                 }
             }
 
             if (touch.phase == TouchPhase.Stationary && swipeSpeed >= walkRunThreshold) 
             {
                 fingerDown = touch.position;
 
                 if (fingerDown.x - fingerUp.x > 0) 
                 {
                     WalkRight ();
                 } 
                 else if (fingerDown.x - fingerUp.x < 0) 
                 {
                     WalkLeft ();
                 }
             }
 
             else if (touch.phase == TouchPhase.Stationary && swipeSpeed < walkRunThreshold)
             {
                 fingerDown = touch.position;
 
                 if (fingerDown.x - fingerUp.x > 0)//Right swipe
                 {
                     RunRight();
                 }
                 else if (fingerDown.x - fingerUp.x < 0)//Left swipe
                 {
                     RunLeft();
                 }
             
             }
         }
     }
         
 
     void WalkLeft()
     {
         transform.Translate(Vector2.left * Time.deltaTime * walk);
     }
 
     void WalkRight()
     {
         transform.Translate(Vector2.right * Time.deltaTime * walk);
     }
 
     void RunLeft()
     {
         transform.Translate(Vector2.left * Time.deltaTime * run);
     }
 
     void RunRight()
     {
         transform.Translate(Vector2.right * Time.deltaTime * run);
     }
 
 }
Comment

People who like this

0 Show 0
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

Answer by jckmull86 · Mar 04, 2018 at 11:06 PM

Hello! I'm still looking for help on this one. Help doesn't have to be fixing my code. If anyone knows of a great all encompassing guide on coding touch gestures, or maybe an asset store game with similar touch features. Something I could dissect.

This is my first question ever so I apologize if I didn't follow the forum guidelines correctly. I'll take any suggestions towards making future inquiries more visible to the community.

Thanks!

Comment

People who like this

0 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 prim200 · Mar 04, 2018 at 11:30 PM 1
Share

Hi sry to say this but not many ppl answer questions here. My advise to you is : 1. If you cant find what you are looking for try describing your question in other ways. 2. If you still cant find it drop the project do something other.

btw if you are looking for a movement for a specific game try typing the name of game and its movement maybe it helps.

out of curiosity do you want to make a game like Temple Run? try typing it in google good luck.

avatar image jckmull86 prim200 · Mar 05, 2018 at 08:00 PM 0
Share

Well I really appreciate you replying at least. I hadn't tried typing in a game with similar controls. That will take some digging because I don't know of any, but it's still helpful.

No it not like temple run. It's a platformer like Ninja Gaiden from the NES days. But I don't want to use a touch controller pad with UI buttons and a joystick. Just swipes and taps on the screen. Thanks again!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

99 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

Related Questions

Check whether touch is held 0 Answers

Custom touch phase? 0 Answers

Swipe Control Issue.. Need help 1 Answer

TouchPhase doesn't end when player swipes off screen 1 Answer

How to get intermediate swipe positions? 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