I am looking again and again for some script that can help me to understand and implement IPhone and IPAD script for capturing the swipe gesture and then translate that velocity and direction into a game object, but with no success.
[Basic Background : Its a bowling game that captures gamers swipe gesture and translate that gestures characteristics into bowling ball that roll down the bowling lane to hit the bottles at the end of lane. ]
You can write a basic script that uses touchCount and GetTouch from Input. Then use the phase and position of the touch to do your math. Save the Began-phase position, then when you detect the Ended-phase, you have all the data you need to produce a velocity.
http://www.unifycommunity.com/wiki/index.php?title=FingerManager
FingerManager can detect touches on a GameObject which has this script attached. (Collider needed)
You can simply use the it to capture a Begin-touch and End-touch/Cancel-touch.
Then you can make a timer to calculate the direction and distance between Begin-touch and End-touch.
If you want to support multi-touch, just define an array to store all the X,Y of the Begin-touch.
When the touch ends, find the closest X,Y in the array to determine which finger the player is using.
Do you think you could elaborate on that a bit? Would I get the position of the Begin-Touch, then get the position of the End-Touch, then draw a ray connecting them and then apply force (based on the amount of time it took for the swipe), based on that ray? If so, how would I do that?
You don’t even have to use ray at all.
1: Set a timer as a threshold to determine a “Swipe” or “Flick”
2: Within the time, Store the Begin-Touch’s X and Y //Vector2
3: Store the End-Touch’s X and Y //Vector2
4: Vector3 EndTouchInWorldSpace = Camera.main.ScreenToWorldPoint (new Vector3 (End-Touch.X,End-Touch.Y, GO.transform.position.z)); //GO is the gameObject that attached FingerManager.
5: Use the above method to get the BeginTouchInWorldSpace also
6: Vector3 SwipeDirection = EndTouchInWorldSpace - BeginTouchInWorldSpace; //This is the direction that you want the GO to follow your swipe direction in world space
7: float SwipeDistanceInWorldSpace = Vector3.Distance(EndTouchInWorldSpace , BeginTouchInWorldSpace);
float SwipeDistanceInPixel = Vector2.Distance(End-Touch , Begin-Touch) //Depends on what swipe distance you want to use
8: Finally, You can use any method to move your GO now
GO.transform.Translate(SwipeDistanceInWorldSpace * SwipeDistanceInWorldSpace * Time.deltaTime); //Don’t forget to put in update!
GO.rigidbody.AddForce(SwipeDistanceInWorldSpace * SwipeDistanceInWorldSpace); //Use Force
iTween.MoveBy(GO, SwipeDistanceInWorldSpace * SwipeDistanceInWorldSpace, Time); //iTween
AnimatePosition(GO, EZAnimation.ANIM_MODE.By, SwipeDistanceInWorldSpace * SwipeDistanceInWorldSpace, EZAnimation.linear, Time, 0, null, null) //EZGUI