- Home /
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.
Answer by MUG806 · Feb 20, 2021 at 10:45 AM
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
}
this kind of works but it's not exactly what I'm looking for. Following your method, as I'm dragging further from where the first touch was, the player travels a longer distance (which I find better than having a constant offset), but the aspect of "speed of drag" is not there. Here is the code: if (touch.phase == TouchPhase.Began) { startPos = transform.position; startTime = Time.time; deltaX = touchPos.x - transform.position.x; deltaY = touchPos.y - transform.position.y; }
if (touch.phase == TouchPhase.Moved)
{
dis = (Vector2)transform.position - startPos;
diffTime = Time.time - startTime;
rb.MovePosition(new Vector2(touchPos.x - deltaX, touchPos.y - deltaY) + dis / 1.5f);
}
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Stationary)
{
rb.velocity = Vector2.zero;
}
I'm trying to let the game respond to the user's fast drag that will fulfil his reflex by moving the player further if he drags faster even if the was a short drag. If it was possible to achieve that, it would be great. But Thanks anyways!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I swipe on a texture and know if its swiped 100% 1 Answer
Make object follow my finger (Touch)? 1 Answer
Bullet not moving from script 3 Answers