Hello there I have a script that moves object in the direction of a swipe but I only need it to go straight up or straight or left to right swipes. I want to restrict diagonal movement when swiping but not sure how to accomplish this
Vector2 startpos, endpos, dir;
float touchstart, touchend, timeint;
public float force;
private Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void Update ()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
touchstart = Time.time;
startpos = Input.GetTouch(0).position;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
touchend = Time.time;
timeint = touchend - touchstart;
endpos = Input.GetTouch(0).position;
dir = startpos - endpos;
rb.velocity = -dir/timeint * force * Time.deltaTime;
}
}