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
I managed to do this by myself
(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 (Devin Curry - YouTube) on touch Input