• 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 PlayKiseki · Nov 13, 2014 at 09:46 PM · inputtouchcontrolsswipe

swipe movement control

Hi there,

I am making a 2D game for touch devices but I'm having some trouble implementing touch controls. I've tried several things such as using Control Freak, coding swipes, etc. but I can't get it to work.

The kind of controls I am aiming for are similar to those in Mage Gauntlet & Wayward Souls, where the player swipes and holds in a direction, which the character gameObject follows. See https://www.youtube.com/watch?v=AjraDTy64BA 00:21 for what I mean.

Can anybody point me in the right direction & help me understand how controls like this work? Any tutorials are welcome! Here is my current script, which is from a tutorial & is not what I'm looking for.

 void Update ()
         {
 
                 float inputModifyFactor = (rigidbody2D.velocity.x != 0.0f && rigidbody2D.velocity.y != 0.0f) ? .7071f : 1.0f;
 
                 if (Input.touchCount > 0) {
                         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.Ended:
                                         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;
                         
                                                 if (Mathf.Abs (direction.x) > Mathf.Abs (direction.y)) {
                                                         //Horizontal swipe
                                                         swipeType = Vector2.right * Mathf.Sign (direction.x);
                                                 } else {
                                                         //Vertical swipe
                                                         swipeType = Vector2.up * Mathf.Sign (direction.y);
                                                 }
                         
                                                 if (swipeType.x != 0.0f) {
                                                         if (swipeType.x > 0.0f) {
                                                                 rigidbody2D.velocity = new Vector2 (10, rigidbody2D.velocity.y * inputModifyFactor);
                                                         } else {
                                                                 rigidbody2D.velocity = new Vector2 (-10, rigidbody2D.velocity.y * inputModifyFactor);
                                                         }
                                                 }
                         
                                                 if (swipeType.y != 0.0f) {
                                                         if (swipeType.y > 0.0f) {
                                                                 rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x * inputModifyFactor, 10);
                                                         } else {
                                                                 rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x * inputModifyFactor, -10);
                                                         }
                                                 }
                                         }
                                         break;
                                 }
                         }
                 }
         }

Thank you so much!

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 alpha_waves · Feb 24, 2017 at 09:47 AM

As this is about 2 years later I'm guessing you've found your answer, but hey!

I'm trying to do Wayward Souls-style controls too for my own sci-fi action rpg

I found this tutorial where the programmer has done something similar to Wayward Souls...

https://www.youtube.com/watch?v=xP7zlw7o51M&t=29s

You can download his Unity project from GitHub in the links on that page.

I'm extending it to add in the right-hand-side swipes and some extra fun mechanics on top.

I would love to see what you ended up doing, and if you have any advice on this for me. Perhaps we could team up if you're interested? I've got lots of ideas for story, mechanics, music, and visuals - I'm just getting my feet wet in Unity at the moment trying to bring them to life and would love your help or to help you on a similar project :)

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Swipe and Hold to move character . 1 Answer

Touch Controls for a 2D ball game 0 Answers

How do you map touch to one of the input axes? 1 Answer

Toggle between Touch input and accelerometer 0 Answers

Detect swipe along 3d arrows 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