Help with iPhoneInput rotating object

Hi,

I am trying to rotate an object based off the finger movement on the iPhone screen. So far, I have the following code.

function Update ()
{
    if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
    // Get movement of the finger since last frame
        var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
        transform.eulerAngles = Vector3(0, touchDeltaPosition.x, 0);
    }
}

This does rotate the object, but it does very quickly. This is based off the tuochDeltaPosition.x set for the Y. However, I’m not sure how else to fix this.

I’d like to be able to move a finger to the left of the screen and have the object rotate in -Y and a finger move to the right would rotate +Y. Also, I’d like for an up and down finger move to the object around the x.

I’d greatly appreciate any help! I can’t seem to find any samples for the iPhone which would help to figure this out.

Thanks,

Wes

The current code changes the rotation each frame so you need to multiply touchDeltaPosition.x by Time.deltaTime to make it framerate-independent. If you find this is now too slow, multiply a second value to increase the speed:-

transform.eulerAngles = Vector3(0, touchDeltaPosition.x * Time.deltaTime * speed, 0);

Thanks for the help. I’m still having the same issue of the object rotating in the Y axis incredibly fast. It looks like the object is just jittering in place. I’m wondering if my approach is very wrong. I’d like to have the object rotate as the user swipes their finger on the iPhone. I’m not sure how to properly translate the touchDeltaPosition.x into the correct value for rotating the object. Here’s what I’ve changed.

public var speed : float = 0.2;
function Update ()
{
    if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
    // Get movement of the finger since last frame
        var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
		transform.eulerAngles = Vector3(0, touchDeltaPosition.x * Time.deltaTime * speed, 0);
	}
}

How about adding some damping instead of setting the rotation right away?
The smooth follow script that comes w unity has some good lines for damping that you may be able to use?
TT

Thanks for the help! I tried use the mathf. SmoothDamp function, but I still get the same erratic rotation behavior. I tried setting touchDeltaPosition.x * Time.deltaTime as the target and var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition; as the current in the smoothDamp function.

I’m not sure what to do from this point. It seems that my issue is with using the touchDeltaPosition and feeding that into the rotation. I think I need to convert the movement of the finger so that it translates as rotation, but I’m not sure how to go about this.

Any ideas?

Thanks,

Wes

Sorry - I’ve just noticed the crucial detail here…

The touch delta value is the distance between the current finger position and the last finger position. The delta will be roughly the same each frame if the finger is moving smoothly. Your script is setting the angle of rotation to this value, so it will just flicker between a few close values rather than accumulating the turns each frame. You need to use:-

transform.Rotate(Vector3.up * touchDeltaPosition.x * Time.deltaTime * speed);

Excellent! Thanks very much. This works perfectly : )

-Wes

Wish I was that good! :wink:
-TT

Yeah, but imagine the responsibility… :wink:

Could anyone provide a suggestion for implementing this damping “instead of setting the rotation right away”?
Would the damping be implemented? In the touch code itself? or a LateUpdate?
Any additional explanations would be appreciated!

A simple way to damp the rotation is to use the lerp function to interpolate between the current rotation and the target rotation:-

transform.rotaton = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime);

This doesn’t give a very precise result but is often enough to give an acceptable softening effect.

This is exactly what I’ve been looking for. Any suggestions as to how to keep the object rotating if the user flicks though?

Just wondering, i understand that this code works for allowing the rotation on the x-axis, but what if we wanted it to also work on the y-axis?

transform.Rotate(Vector3.up * touchDeltaPosition.x * Time.deltaTime * 5);

Thats the code so far for just the x-axis, so is there a possibility to make an addition to say touchDeltaPosition.y?

##EDIT##

Ignore this post, i just solved it; here is the new line if you want to do x-axis and y-axis;

transform.Rotate(Vector3(touchDeltaPosition.y, touchDeltaPosition.x, 0) *Time.deltaTime *5);

5 = the speed

BCW0012: WARNING: ‘UnityEngine.iPhoneInput.GetTouch(int)’ is obsolete. GetTouch method is deprecated. Please use Input.GetTouch instead.

Need some more help than stating the obvious?

yes, sorry, how do i get this working with the new methods… im also using vuroria. im no coder so in lamens please