- Home /
 
(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
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
actually no, now that I notice that isn't based on screen percentage, seems that I still need help
Your answer
 
             Follow this Question
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