Rotating in relation to touch X on screen

Hello everyone !

It’s been 3 days I’m stuck on this problem which should in theory be simple. I’ve tried many different options but without success. I figure a more experienced programmer here might have the elegant solution to this.

Basically I have my camera nested as a child, with a 0f, -0.9f, 0f local position to a parent “rotater” object at the center of the world. That object rotates on X. While to player applies a touch on screen, I’m calculating something like:

``````float perc = Input.mousePosition.x/Screen.width;

float rotaterXRot = 360f * perc;

rotater.transform.rotation = Quaternion.Euler(new Vector3(rotaterXRot, 0f,0f));
``````

(I do substract the delta of the touch and the actual equivalent on screen X in % of the rotater’s rotation, so when I press the screen the object doesnt “teleport”, and left or right motion will only rotate from the rotation the object is at at the time of the touch.)

It works fine left and right until 90 degrees if I move to the right and 270 if I move to the left. Once I overshoot that 180f around the start position, the rotation jumps to weird values.

My question is, is there any simple way for me to translate my touch.x in % of the screenwidth to rotate my object along a 360f angle where 180f is where the x rotation is at the time of the touch (If when I press my object is currently rotated to -174 degrees on x, this should mark the 180f in my 360f) ?
(here is a graphic to explain more explicitly the situation:

Edit: I do take the current localEulerAngles as a vector3 at the time of the press, then add it to the % of 360f rotation according the the touch X pos, to finally enter these two as the new target Rotation for the object:

``````lastRot = rotater.transform.localEulerAngles;
// then later
float rotX = percOfTouchX * 360f;
rotX += 180f;
Vector3 tempV3 = new Vector3 (adjustedRot, 0f, 0f);
rotater.transform.rotation = Quaternion.Euler (tempV3+lastRot);
``````

This works but again the problem is when I touch 90 degrees, the angle goes down to zero instead of continuing past 90 —> …88, 89, 90, 88, 89…
Same thing for 270. I believe this what is causing my problem. It’s also the value that printing rotater.transform.eulerAngle.x gives me. Any idea how I could avoid this please ?