Relative rotation with touch sometimes jumps to new rotation

Hello,I have circle in my game that i use to rotate while draging my finger on the screen.When i take finger off the screen,and then drag from another place,the circle should continue rotating from previous rotation instead of instantly changing rotation.It kinda works but my problem is that sometimes - I can’t tell when that happens exactly,the circle jumps to new rotation instead of continuation.Can someone tell me where i have error in my script?

Here’s the exact part of the script :

if (Input.touchCount > 0)
        {

            Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            Vector2 touchPos = new Vector2(pos.x, pos.y);
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                startAngle = Mathf.Atan2(touchPos.y,touchPos.x) * Mathf.Rad2Deg;
                if (startAngle <= 0)
                {
                    startAngle += 360;
                }
            }
            angle = Mathf.Atan2(touchPos.y, touchPos.x) * Mathf.Rad2Deg;
            if(angle < 0)
                angle += 360;
            distanceAngle = startAngle - endAngle;
            angle -= distanceAngle;
            if (angle < 0)
                angle += 360;
            else if (angle > 360)
                angle -= 360;
            if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                endAngle = angle;
            }
            
            circle.rotation = Quaternion.Euler(0f, 0f,angle);

Let’s break this down quickly:

Your object current has no rotation, so zero degrees.

You press your finger down at 45 degrees and circle around to 90 degrees. Your starting rotation is now 45 and your ending rotation is now 90.

You press your finger down at 180 and rotate around to 270. Your new starting angle is 180 and your ending angle changes from 45 to 135.

Going back to the start, that means you cancel out the difference between your finger’s angle and your starting angle 45 - 45 = 0. Then, when you release your finger, the final rotation is 45 degrees, based around endAngle = angle - startAngle.

For the second set, you’re now updating the starting angle. 180 - 180 = 0 The object is currently at 45 degrees, therefore your rotational sum comes from current - start + end or, in this case, 180 - 180 + 45. Rotate it around and you now have 270 - 180 + 45 for a 135 degree rotation.

With all the logic in mind, you almost had it right. A single oversight resulted in the following line:

endAngle = angle;

Your ending angle is not accommodating any starting angle aside from 0 degrees. Instead, it should read:

endAngle = angle - startAngle + endAngle;