# Getting trigonometry compatible rotation and position

Here’s my problem: I’m trying to get the coordinates of a iOS touch, and then rotate a GameObject towards that object. I’m trying to work in two dimensions so I’m getting the xy coordinates of the touch and rotating my GameObject on the Z axis accordingly. Here’s my javascript/pseudo-code:

``````function Update() {

if(Input.touchCount > 0) {
touchx = Input.GetTouch(0).deltaPosition.x;
touchy = Input.GetTouch(0).deltaPosition.y;
delta_x = transform.position.x - touchx;
delta_y = transform.position.y -  touchy;

// get the arctan of d_x/d_y and add 90 to it
// add 90 to it because I want 90 degrees to  be 0 degrees
transform.Rotate(0,0,arctan(delta_x/delta_y) in degrees + 90)
}

}
``````

I am aware that something extremely simple is wrong with the transform.Rotate call. I have tried different things and nothing seems to work. When I run this code the GameObject just spins in a circle. I tried subtracting transorm.eulerAngle.z from the arctan and taking the results modulo of 360 but it still doesn’t behave properly. Help would be greatly appreciated.

Quick guess is that arctan is giving you radians. Convert to degrees. C# Convert Radians Degrees - Visual C# Kicks

hang on a minute … as well as any other problems,

are you totally mixing up GLASS positions and WORLD positions ?

when you say somethign like transform.position.x that is a position in the real universe (I mean, in your game’s real universe !)

when you say GetTouch…position that is purely a GLASS position.

it’s a basic of writing input code that you cannot mix the two, of course.

you have to EITHER convert everything to GLASS paradigm, or convert everything to REAL paradigm, before prcoeeding.

(some things you can do with “only the GLASS” paradigm … but really not much, it’s almost always a simplification if you try t do that.)

it’s remarkably difficult writing really good input code. it is “haiku programming”. the trivial hello world examples you always see are really misleading. For example I notice you have Input.touchCount > 0, unfortunately, that is not really suitable for real-world use. what happens if the person happens to be dragging a thumb on the edge of the screen etc, what happens if the number of touches changes during a swipe, how do you know which touch you’re following, and so on. a good simplification is ==1, which’ll get the ball rolling. but in “final production code” you really have to worry about every possible corner case. (particularly if the app is for small children, BTW) it’s nuts really!

PS you may enjoy this very long answer here