2D circle rotation by touch

I struggle to create correct rotation of a circle. Its a mobile game, in the center of the screen is the circle and players can rotate it around Z axis by touch. It must rotate by touching any part of the screen. Ill appreciate your help with this one, hope here are some mobile gaming gurus)

Below is my half-working example:

void Update()
    {

        if (Input.touchCount > 0)
        {
            theTouch = Input.GetTouch(0);

            if (theTouch.phase == TouchPhase.Began)
            {
                touchStartPosition = theTouch.position;
            }

            else if (theTouch.phase == TouchPhase.Moved || theTouch.phase == TouchPhase.Ended)
            {
                if (touchEndPosition == theTouch.position) return;
                touchEndPosition = theTouch.position;
               
                float x = touchEndPosition.x - touchStartPosition.x;
                float y = touchEndPosition.y - touchStartPosition.y;

                double distance = Math.Sqrt(x * x + y * y);
                float f = Convert.ToSingle(distance);

                f /= screenSensitivity;
                Quaternion pos = circle.transform.rotation;

                Vector2 endPoint = cam.ScreenToWorldPoint(new Vector3(touchEndPosition.x, touchEndPosition.y, cam.nearClipPlane));
                Vector2 startPoint = cam.ScreenToWorldPoint(new Vector3(touchStartPosition.x, touchStartPosition.y, cam.nearClipPlane));

                Vector3 circlePos = circle.transform.position;

                if ((Mathf.Abs(y) > Mathf.Abs(x) || (Mathf.Abs(y) == Mathf.Abs(x))))
                {

                    if (y < 0)
                    {
                        f *= -1;
                    }

                    if (endPoint.x > circlePos.x)
                    {
                            f *= -1;    
                    }

                    f *= -1;

                } else
                {
                    if (x < 0)
                    {
                        f *= -1;
                    }

                    if (endPoint.y > circlePos.y)
                    {
                        f *= -1;
                    }
                }

                currentRotation += new Vector3(0, 0, circle.transform.rotation.z + f);          
                circle.transform.localEulerAngles = currentRotation;

                touchStartPosition = touchEndPosition;
            }
        } 
    }

9432656--1322588--Screenshot_18.png

Attached is my reference setup for this, fully set up in a scene.

9648056–1372379–RotateZViaDrag.unitypackage (20.1 KB)

ALSO: you don’t need this nonsense:

Just use Mathf.Sqrt if you need a float out!!!

float distance = Mathf.Sqrt( x * x + y * y);

Or even use Vector2.Distance:

float distance = Vector2.Distance( touchStartPosition, touchEndPosition);

Or just use the .magnitude property:

float distance = (touchStartPosition - touchEndPosition).magnitude;

Thank you, brother! You saved my life.