I’m having some issues tilting a platform left or right on the Z axis based on touch. What I need it to do is take the position of the touch, and tilt the object to a max angle I set. I’m having an issue as touch.position returns 0 on left side of screen and 360 on the right side of the screen. How can I get the center of screen to = 0, and moving to left or right would = positive or negative number based off the zero of the screen?
This issues comes up in all manner of graphics work, not just with touch and Unity.
This is under the general notion of altering the coordinate system, known as transforming (sometimes just translating) from one coordinate system to another.
For example, you want the screen center to be 0, 0, but the display is known to the operating system whereby the upper left corner is 0, 0, with positive X to the right and positive Y moving down.
That is contrasted by some systems which assume 0, 0 is the lower left corner, positive X is to the right and positive Y moves upwards.
You’ll note in those two descriptions, the second is close to the traditional Cartesian graph, limited to the upper right quadrant where all values are positive, but the first one is inverted in Y.
I show this so you can see that it may not just be that you must convert the center to your expectations, in some situations you must invert an axis to suit your expectations (this depends on your target, your platform, etc).
So, let’s focus specifically on your X coordinate inquiry (left to right). It appears you have a display that offers touch from 0 to 360, where the center is therefore 360 / 2 = 180. If you subtract 180 from the input coordinate, the result will be 0 at the display center, -180 at the left, and 180 at the right.
If you expand that into the Y coordinate, you’ll need to know the extent (the 360 in the X example), generally you divide by 2 and offset similarly. The exception comes if you must also invert the Y axis. In that one case, let’s say Y runs from 0 to 780, but is inverted. You then subtract the input from 780, which inverts, then divide by 2, which centers. That is if and only if you require inversion.