Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AaEsS · Feb 20, 2021 at 10:23 AM · c#speeddragtouchscreenswipe

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AaEsS · Feb 20, 2021 at 11:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Touch Script - Fix GameObject touch. 0 Answers

C# 2D InfiniteRunner run with same speed even when going up 1 Answer

I need your help with this system of particles, good with this code 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges