[MOBILE 2D] Move object relative to finger in x axis

Hi, I have really low experience with unity, and I’ve been looking everywhere for a solution to this.

How do I make a 2d object move relative to the finger, in smooth and natural way? It sounds like a relatively simple thing to code for me, but I just haven’t gotten it right yet.

I’ve tried my own methods, which were quite bad, and right now I’m using one I found on an old thread over here:

    if (Input.touchCount == 1)
    {
        Touch touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Moved && gameStarted)
        {
            transform.Translate(touch.deltaPosition.x/Screen.width * velocityConstant, 0.0f, 0.0f);
            transform.position = new Vector2(Mathf.Clamp(transform.position.x, leftLimit, rightLimit), transform.position.y);
        }
    }

It gets the job done sometimes, but it’s so inconsistent and unpredictable, it really blows the experience of the game I’m trying to make. Sometimes it moves just a small distance, and then the opposite. My first guess was that it was a framerate problem, but FixedUpdate didn’t solve it.

Is there anything in particular I could change to this code to make the movement solid and consistent? Please help!

Your help will be greatly appreciated, Thank you!

Well
Store the position on touchphase start, subtract the current position of the position on touchphase move from the started touch position.

		if (Input.touchCount > 0) {
			var touch = Input.GetTouch(0);
			switch (touch.phase) {
				case TouchPhase.Began:
					startPos = touch.position;
					break;
				
				case TouchPhase.Moved:
					distance = touch.position - startPos;
					break;
			}
		}

The distance would then just be applied to the transforms position x.