Smooth rotation issue

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.

If I were to do this I would calculate the the angle to the touch positions relative to the x-axis with the object center, or the center of the touches, as the origin. Store this value and then calculate the angle change next frame and apply it to the object rotation.

Right now it looks like your plugging in a vector2 as a float in the rotate function. Does that even work? Try taking the magnitude of the vector instead.

Edit: Sorry I should have read the code better. You are taking the x value or the calcRot Vector2 and using it as a rotation amount. I would really recommend going the angle route as it will ‘feel’ much more realistic.

Edit: Edit: Check out Vector3.Angle. You don’t even need to do your own angle calculations!

Thanks, I’ll play with it and report back.