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.