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 samcace · Sep 17, 2017 at 11:13 AM · 2d-platformercharacter movementmobile devicesswipe

How can i use single swipe to run a character continuously.

I am making a 2d platformer game and am able to swipe but character only moves one step. I want a single swipe to make character run continuously, another swipe changes direction. here is the section of code responsible for detecting swipe action

   void Update()
 {

     if (Input.touchCount == 1) // user is touching the screen with a single touch
     {
         Touch touch = Input.GetTouch(0); // get the touch
         if (touch.phase == TouchPhase.Began) //check for the first touch
         {
             fp = touch.position;
             lp = touch.position;
         }
         else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
         {
             lp = touch.position;
         }
         else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
         {
             lp = touch.position;  //last touch position. Ommitted if you use list
 
             //Check if drag distance is greater than 20% of the screen height
             if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
             {//It's a drag
              //check if the drag is vertical or horizontal
                 if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                 {   //If the horizontal movement is greater than the vertical movement...
                     if ((lp.x > fp.x))  //If the movement was to the right)
                     {   //Right swipe
                         Debug.Log("Right Swipe");
                         float move = 1f;    //movement in the right direction

                         if (move > 0 && !facingRight){
                             Flip();
                         }
                         Move(move);
                         

                     }
                     else
                     {   //Left swipe
                         Debug.Log("Left Swipe");
                          float move =-1f;   //movement in the left direction
                         if (move < 0 && facingRight){
                             Flip();
                         }
                         Move(move);
                     }
                 }
                 else
                 {   //the vertical movement is greater than the horizontal movement
                     if (lp.y > fp.y)  //If the movement was up
                     {   //Up swipe
                         Debug.Log("Up Swipe");
                     }
                     else
                     {   //Down swipe
                         Debug.Log("Down Swipe");
                     }
                 }
             }
             else
             {   //It's a tap as the drag distance is less than 20% of the screen height
                 Debug.Log("Tap");
                 anim.SetBool("Ground", false);
                 m_Rigidbody2D.AddForce(new Vector2(0f, jumpForce));
             }
         }
     }
 }

And this is the section of code responsible for moving the character

         void Move(float playerDirection)
     {
         anim.SetFloat("speed", Mathf.Abs(playerDirection));        
 
              // Moves Character 
         m_Rigidbody2D.velocity = new Vector2 (playerDirection*maxSpeed, m_Rigidbody2D.velocity.y);
     }

And this is the section of code responsible for changing character direction

 void Flip ()
 {
     facingRight = !facingRight;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }




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

0 Replies

· Add your reply
  • Sort: 

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

71 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

Related Questions

Decrease a value 0 Answers

How to change the movement speed of a character 4 Answers

Object is passing from colliders when swipe with rigidbody.position 1 Answer

Allow 2D character overlaps in side-scroller? 1 Answer

Unity mobile delta position not working as expected 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