How to continuously make the player travel a certain amount of distance based on the drag speed on screen (just like on trackpads on laptops)

Hi, so I’m trying to make the player travel some amount of distance based on the speed at which the user drags his finger along the screen. In laptops, we find this behaviour when using a trackpad (or touchpad) where if you were to drag faster, the cursor goes a longer distance than if you were to drag slower. I think a perhaps big problem regarding this question is how to constantly keep calculating the speed of finger drag and updating the distance to travel each time.

The code below does implement the dragging but always the same offset between the player and the user’s finger is maintained from when the finger touches the screen until lifted (which is sign of what I’m trying to achieve is not implemented, rather the player travels the same distance covered by the distance the finger travels)

inside Update() method:

if (Input.touchCount > 0)
{
    Touch touch = Input.GetTouch(0);
    Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);

        if (touch.phase == TouchPhase.Began)
            {
                startPos = transform.position; // unaffecting
                startTime = Time.time; // unaffecting
                deltaX = touchPos.x - transform.position.x;
                deltaY = touchPos.y - transform.position.y;
            }

            if (touch.phase == TouchPhase.Moved)
            {
                rb.MovePosition(new Vector2(touchPos.x - deltaX, touchPos.y - deltaY));
            }

            if (touch.phase == TouchPhase.Ended)
            {
                rb.velocity = Vector2.zero;
            }
}

Essentially, I’m trying to make what the trackpads and touchpads can do regarding dragging but with touchscreen.

.

Thank you in advance.

So I don’t know anything about the code specific to getting touch info, as I’ve not worked with those devices, but I think I can help you with the logic. If I’m understanding correctly, you want the object to move further if you drag the same distance across the screen, but at a higher speed? Here’s a pseudo-code example of how I would approach this:

Vector2 lastTouchPosition
When the user first touches{
     Store the touch position in lastTouchPosition
}

Every frame while user is dragging{
    Calculate distance between last touch position and current touch position.
    Square that distance (or raise it to another power using Mathf.Pow)
    //^ this will cause the distance to increase exponentially the larger it was, which sould get your ramping up speed effect.
    Move the object to its current position + the distance we calculated.
    update the stored lastTouchPosition with the new touchPosition
}