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 MSachs · Jan 24 at 12:57 PM · dragsmoothmapslow down

Drag image and slow movement down after letting go?

Hi,

I am using the following script to drag a 2D image arround within a min and max position value (basically a map).


     public void OnDrag(PointerEventData eventData)
     {
         transform.localPosition += (Vector3)eventData.delta * 2f;
     }
 
     void Update()
     {
         Vector3 currentPosition = transform.localPosition;
 
         currentPosition.y = Mathf.Clamp(currentPosition.y, minY, maxY);
         currentPosition.x = Mathf.Clamp(currentPosition.x, minX, maxX);
         transform.localPosition = currentPosition;
     }

I would like to not have the image come to a hard stop after letting go but smoothly slow it down over like 1 or 2 seconds. Any help how to achieve this is much appreciated.


Thanks 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 NPnpNPnpNP · Jan 25 at 10:20 PM

You can use Vector3.Lerp() to move the image, and combine it with an Animation Curve to adjust the speed over time.

Here a good example of how I achieve this kind of animations in my games :

     public AnimationCurve animationCurve;
 
     public void StartMove(Vector3 finalPosition)
     {
         StartCoroutine(Move(transform.position, finalPosition, 2));
     }
 
     private IEnumerator Move(Vector3 origin, Vector3 finalPosition, float duration)
     {
         for (float i = 0; i < duration; i += Time.deltaTime)
         {
             transform.position = Vector3.Lerp(origin, finalPosition, animationCurve.Evaluate(i / duration));
             yield return null; // Returning null here allows to wait for the next update.
         }
     }


Comment
Add comment · Show 5 · 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 MSachs · Jan 26 at 01:20 PM 0
Share

Thanks for your answer!

But i don't really get how that could work in my case... Wouldn't I need the "finalPosition" before i drag the map but it would change depending where and when I let go of the map...

avatar image NPnpNPnpNP MSachs · Jan 26 at 02:08 PM 0
Share

Sorry my English is not perfect so I'm not sure that I understand what you're trying to do. Do you have a video to illustrate it ?

avatar image MSachs NPnpNPnpNP · Jan 26 at 03:09 PM 0
Share

yeah, sure.

https://www.youtube.com/watch?v=hUtX-FX1IFU

So I have the red square as a placeholder for my map and I am dragging it around using the script I put in my question above. The thing I want to achieve is that after I let go of the mouse button (ending the dragging) I don't want the red square to stop immediately like it does in the video but I would like it to slow down slowly. Just like it does on Google Maps or Apple Maps for example.

Show more comments

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

136 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 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

[Solved] Move camera with mouse dragging (2D) 1 Answer

Assigning UV Map to model at runtime 0 Answers

Smooth material of cylinders 2 Answers

How can I make GUI labels Draggable? 2 Answers

Smooth rotation when dragging on mobile(android) 2 Answers


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