Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MadJohny · Dec 28, 2013 at 03:14 PM · androidtouchdirectionswipe

(Android) Swiping direction

Hi, I made a simple swipe function for my game, the problem is, it doesn't ask for any direction, as long as the distance traveled is equal to 25% of the screen, but now I want to know what direction the player swiped, and apply force to an object based on the swipe direction, so basically the force would be applied like this: rigidbody2D.AddForce(Vector2.up force swipeDirection) being swipe direction -1 or 1.

So if the player swipes up, it would be (Vector2.up force 1) and if he swipes down it would be * -1, I hope you understand what I want and I hope someone can tell me how to achieve this.

This is my code btw: (void OnTouchMoved is called when the TouchPhase is TouchPhase.Moved

 void OnTouchMoved () {
     if (coolDown >= 5f) {
         if (Input.GetTouch(currTouch).deltaPosition.y  >= 0.25f) {
             coolDown = 0f;
             print ("swiped");
         }
     }
 }

Thanks in advance.

edit: Also seems that the swiping is only working when I swipe from bottom to top and not from top to bottom

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
1

Answer by MadJohny · Dec 28, 2013 at 03:30 PM

I managed to do this by myself :D (nope I didn't, check my reply to this answer) Here's the script if any of you want it:

 using UnityEngine;
 using System.Collections;
 
 public class AgileSpecial : TouchLogic {
     
     public float force;
     public float coolDown = 0f;
     public GameObject player;
     bool dodge = false;
     float direction;
     float up = 1f;
     float down = -1f;
 
     void LateUpdate () {
         coolDown = Mathf.Clamp(coolDown,0f,5f);
         coolDown += Time.deltaTime * 1f;
     }
 
     void OnTouchMoved () {
         if (coolDown >= 5f) {
             if (Input.GetTouch(currTouch).deltaPosition.y  >= 0.25f) { //swipe up
                 coolDown = 0f;
                 direction = up;
                 dodge = true;
             }
             if (Input.GetTouch(currTouch).deltaPosition.y  <= -0.75f) { //swipe down
                 coolDown = 0f;
                 direction = down;
                 dodge = true;
 
             }
         }
     }
 
     void FixedUpdate () {
         if (dodge == true) {
             player.rigidbody2D.AddForce(Vector3.up * force * direction);
             dodge = false;
         }
     }
 }

ps: this script derives from another script, (TouchLogic) if you want that TouchLogic script, follow this guy's tutorials (http://www.youtube.com/channel/UC0Ahv9Y12r2To-syiUoR8Lg) on touch Input

Comment
Add comment · Show 1 · 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 MadJohny · Dec 28, 2013 at 07:37 PM 0
Share

actually no, now that I notice that isn't based on screen percentage, seems that I still need help

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

19 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

Related Questions

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

Detect if finger lifted off screen 1 Answer

Android: Get Direction from touch 0 Answers

Android Drag By Steps 0 Answers

Swipe control for circle (Can do swipe left/right) 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