Swipe horizontally and hold finger down to move character?

Hi!

Working on my first project, and I hit a wall with my character controller. The game is an ios Mobile 2D side scroller.

The mechanic has a few parameters.

If the player swipes left to right, and keeps finger down, and the swipe speed is slow enough, the character walks. But if the swipe speed is fast enough, they run.
If the player swipes back to the left, with all the same parameters, the character walks or runs to the left depending on swipe speed.

Below is as far as I could get with my limited scripting knowledge and online research.
I succeeded in moving the character left and right with the swipe but here are my problems.

  • Character shakes as it moves.
  • The finger stationary statement calls the run speed no matter if the finger arrived there on a slow swiping speed.

You can drop the script on an object and observe what I’m seeing.
Thank you for any help, and if you have time, please explain what you’re doing to make it work. I really want to get better at this.

public class secondTest : MonoBehaviour 
{
	private Vector2 fingerDown;
	private Vector2 fingerUp;

	public float walk = 2f;
	public float run = 10f;
	public float walkRunThreshold = 5f;



	// Update is called once per frame
	void Update()
	{

		foreach (Touch touch in Input.touches)
		{
			float swipeSpeed = touch.deltaPosition.magnitude / touch.deltaTime;

			if (touch.phase == TouchPhase.Began)
			{
				fingerUp = touch.position;
				fingerDown = touch.position;
			}

			//Detects Swipe while finger is still moving
			if (touch.phase == TouchPhase.Moved && swipeSpeed >= walkRunThreshold) 
			{
				fingerDown = touch.position;

				if (fingerDown.x - fingerUp.x > 0) 
				{
					WalkRight ();
				} 
				else if (fingerDown.x - fingerUp.x < 0) 
				{
					WalkLeft ();
				}
			}

			else if (touch.phase == TouchPhase.Moved && swipeSpeed < walkRunThreshold)
			{
				fingerDown = touch.position;

				if (fingerDown.x - fingerUp.x > 0)//Right swipe
				{
					RunRight();
				}
				else if (fingerDown.x - fingerUp.x < 0)//Left swipe
				{
					RunLeft();
				}
			}

			if (touch.phase == TouchPhase.Stationary && swipeSpeed >= walkRunThreshold) 
			{
				fingerDown = touch.position;

				if (fingerDown.x - fingerUp.x > 0) 
				{
					WalkRight ();
				} 
				else if (fingerDown.x - fingerUp.x < 0) 
				{
					WalkLeft ();
				}
			}

			else if (touch.phase == TouchPhase.Stationary && swipeSpeed < walkRunThreshold)
			{
				fingerDown = touch.position;

				if (fingerDown.x - fingerUp.x > 0)//Right swipe
				{
					RunRight();
				}
				else if (fingerDown.x - fingerUp.x < 0)//Left swipe
				{
					RunLeft();
				}
			
			}
		}
	}
		

	void WalkLeft()
	{
		transform.Translate(Vector2.left * Time.deltaTime * walk);
	}

	void WalkRight()
	{
		transform.Translate(Vector2.right * Time.deltaTime * walk);
	}

	void RunLeft()
	{
		transform.Translate(Vector2.left * Time.deltaTime * run);
	}

	void RunRight()
	{
		transform.Translate(Vector2.right * Time.deltaTime * run);
	}

}

Hello! I’m still looking for help on this one. Help doesn’t have to be fixing my code. If anyone knows of a great all encompassing guide on coding touch gestures, or maybe an asset store game with similar touch features. Something I could dissect.

This is my first question ever so I apologize if I didn’t follow the forum guidelines correctly. I’ll take any suggestions towards making future inquiries more visible to the community.

Thanks!