- Home /
Turning touch to rotation
Hello!
So my game features an object in the center of the screen, and based on where the player touches I want it to rotate the object.
Ex.
Touch in bottom left hand side will rotate the object on it's left side to make it see as if it's being picked up on that side by the player's touch. (C# references please)
How would I go about making this happen? Thank you!
Answer by podmaster · Nov 06, 2014 at 01:04 AM
Sorry i don't use C# only JS , but you can do this converting the touch cords from screen to world position.
Reference: http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
Then compare the touch cords with the object cords and for say: If object.X is greater than touch.x it means the touch was made on the left side of the screen. If the object is at 0,0,0 an the touch on -50,0,0 ... Same for Y then you can apply the rotation.
You can also check if the touch is on the left/top , right/bottom side of the screen, if the screen is 1980 x 1080 px , and the touch was at 435px,0 then its on the left, after that you can apply the rotation.
float x = Input.GetTouch(0).deltaPosition.x;
float y = Input.GetTouch(0).deltaPosition.y;
float speed = 0.09f;
table.rotation *= Quaternion.AngleAxis(x*speed, Vector3.up);
table.rotation *= Quaternion.AngleAxis(y*speed, Vector3.right);
Using this, but I don't want it to push against the ground and then push backwards, like it's doing.