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!

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.
        }
    }