Hello there,
I’ve been struggling with the following issue for quite some time. Maybe some unity pros can help me out.
For my current project, I need to code a gesture. Imagine this: You hold a screw between your thumb and your index finger. Now put your thumb and index finger on your iPhone screen and try to screw it in. This gesture will allow the user to rotate an object. It will actually simulate grabbing and rotating it.
I haven’t had much trouble, coding the gesture itself. So, let me share some code with you:
var touch1Began : iPhoneTouch;
var touch2Began : iPhoneTouch;
function LateUpdate () {
for (var touch : iPhoneTouch in iPhoneInput.touches) {
if (iPhoneInput.touchCount == 2) { //only if we have two fingers on screen
if (touch.phase == iPhoneTouchPhase.Began) {
touch1Began = iPhoneInput.GetTouch(0);
touch2Began = iPhoneInput.GetTouch(1);
}
else if (touch.phase == iPhoneTouchPhase.Moved) {
var touch1Moved : iPhoneTouch = iPhoneInput.GetTouch(0);
var touch2Moved : iPhoneTouch = iPhoneInput.GetTouch(1);
var calcRot : Vector2 = (touch1Moved.position - touch1Began.position);
transform.Rotate(0, 0, -calcRot.x * Time.deltaTime, Space.World);
}
}
}
}
Let me explain, some of the coding above. When the user taps the screen with two fingers, I gain information about these touches via touch1Began and touch2Began. Once the user starts moving, I use toch1Moved and touch2Moved for input information during the movement.
Now I calculate a new Vector, called calcRot. Next step is to use the calcRot.x value to tell the object to rotate. I use the same basic concept for rotating around x and y axis, using just one finger. This works just fine and super smooth. However, when trying to rotate around z axis, the rotation kinda jumps. Maybe i need to interpolate between values, to get a smoother rotation?
Any help and suggestions are most welcome! Thanks in advance.