How to detect swipe left right up and down

  • here is my code it works but i cant figure out how to make hiim move left right up and down depending on a speed variable. all the events happen in the end section can anyone fix my code so i can move my character? thanks!
  • private var fp : Vector2; // first finger position
    private var lp : Vector2; // last finger position
    var up : Sprite;
    var down : Sprite;
    var right : Sprite;
    var left : Sprite;
    var speed =3;

public var spacing = 1.0;
private var pos : Vector3;
function Update()
{

for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{

if((fp.x - lp.x) > 80) // left swipe
{
pos.x -= spacing;
GetComponent(SpriteRenderer).sprite = left;

}
else if((fp.x - lp.x) < -80) // right swipe
{
pos.x += spacing;
GetComponent(SpriteRenderer).sprite = right;

}
else if((fp.y - lp.y) < -80 ) // up swipe
{
GetComponent(SpriteRenderer).sprite = up;
pos.y += spacing;
}
else if((fp.y - lp.y) > 80 ) // down swipe
{
GetComponent(SpriteRenderer).sprite = down;
pos.y -= spacing;
}
}
}

}

Step 1: Use code tags Using code tags properly - Unity Engine - Unity Discussions

Now, depending on how you want to do it, will depend on what you should do. Usually for me, it’s about two things. Distance and position. This is assuming a person taps, swipes, and then removes their finger. (you may also have pages that turns with the swiping).

Usually I check distance of swipe first to see if the finger traveled a certain amount to register as a swipe.
Then I check position. You can check where a person started their tap and determine if starting x is > or < then ending x for example which will help you determine which direction a person is swiping.