My character moving always to the right everytime i swipe and i have no idea why
using UnityEngine;
using System.Collections;
public class Swipe : MonoBehaviour
{
public float maxTime;
public float minSwipeDist;
float startTime;
float endTime;
float swipeDistance;
float swipeTime;
Vector3 startPos;
Vector3 endPos;
private Rigidbody _rigidBody;
//axel crap
public float speed = 3f;
void Start()
{
_rigidBody = GetComponent<Rigidbody>();
_rigidBody.AddForce(Vector3.right * 200);
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startTime = Time.time;
startPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
endTime = Time.time;
endPos = touch.position;
swipeDistance = (endPos - startPos).magnitude;
swipeTime = endTime - startTime;
if (swipeTime < maxTime && swipeDistance > minSwipeDist)
{
swipe();
}
}
}
}
void swipe()
{
Vector2 distance = endPos - startPos;
if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y))
{
Debug.Log("Horizontal Swipe");
_rigidBody.AddForce(Vector3.right * 200);
}
else if (Mathf.Abs(distance.y) < Mathf.Abs(distance.y))
{
Debug.Log("Vertical Swipe");
_rigidBody.AddForce(Vector3.left * 200);
}
transform.position = Vector3.Lerp(transform.position, endPos, speed * Time.deltaTime);
}
}