How to smooth out the camera movement?

Hi

I found this script online for an orbiting camera using touch controls and it works perfectly fine. But I would like the movement to be a little bit smoother and not stop instantly when lifting the finger.
Anyone got an idea how to do that?

{
    private float x;
    private float y;
    public float xspeed;
    public float yspeed;
    private Touch touch;
    public float distance = 10;
    public Transform target;
    Rect margin;

    public float perspectiveZoomSpeed = 0.1f;
    public float orthoZoomSpeed = 0.1f;
    public float maxZoom = 10f;
    public float minZoom = 2f;


    // Use this for initialization
    void Start () {

    }


    // Update is called once per frame
    void Update ()
    {
        // If there are two touches on the device...
        if (Input.touchCount == 2)
        {
            // Store both touches.
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);

            // Find the position in the previous frame of each touch.
            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

            // Find the magnitude of the vector (the distance) between the touches in each frame.
            float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

            // Find the difference in the distances between each frame.
            float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

            // If the camera is orthographic...
            if (GetComponent<Camera>().orthographic)
            {
                // ... change the orthographic size based on the change in distance between the touches.
                GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;

                // Make sure the orthographic size never drops below zero.
                GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
            }

            else
            {
                // Otherwise change the field of view based on the change in distance between the touches.
                GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;

                // Clamp the field of view to make sure it's between 0 and 180.
                GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 30.0f, 80.0f);
            }
        }


        margin = new Rect(Screen.width * 0.25f, 0, Screen.width, Screen.height * 1.0f);
        float count = Input.touchCount;
        for (int i = 0; i < count; i++)
        {
            Touch tc = Input.GetTouch(i);
            if(margin.Contains(tc.position))
            {
                x += tc.deltaPosition.x * xspeed * 0.02f;
                y -= tc.deltaPosition.y * yspeed * 0.02f;
            }
        }
        Quaternion rotation = Quaternion.Euler (y, x, 0);
        Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + target.position;

        transform.rotation = rotation;
        transform.position = position;
    }
}

thanks in advance!

Simple change…

Try changing Update to LateUpdate. This causes all this to run right before the frame is drawn.

Thanks that helped. But it still seems a little static… How would I make it lag behind a little or make it more dynamic so it doesn’t stop instantly when the finger is lifted?

Only semi-related but I would look into Cinemachine for camera controls. It has the ability to keep the target within a selected frame area and smooth out motions without a lot of work from the developer. It’s a bit overkill for this specific scenario, but it would let you forego nearly all of the manual coding needed to get the result you seem to want, and has a ton of other features besides.

Just an idea. Check out this Unite presentation for a ton of information, or this one for a quick demonstration of using it with a 2D game. It works for 3D as well, no worries, just not a lot of videos for it yet.