I want to drag my player on dragging of touch but only on x-axis (left and right) and on z-axis (up and down) but limited to 10 to 16 on x-axis and -20 to -23 on z-axis.
- when user drag finger on left, player should move left in x-axis.
- when user drag finger on right, player should move right in x-axis.
- when user drag finger to up, player should move left in z-axis.
- when user drag finger to down, player should move right in z-axis
I used this code but it only can move object once on every swipe. i want to drag my object as user dragging his finger. Please Help
#pragma strict
private var fp : Vector2; // first finger position
private var lp : Vector2; // last finger position
var playerposition : float;
function Update () {
playerposition = transform.position.x;
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
if((fp.x - lp.x) > 80) // left swipe
{
//if (playerposition > 20) {
transform.Translate(Vector3.left * 15 * Time.deltaTime);
//}
}
else if((fp.x - lp.x) < -80) // right swipe
{
//if (playerposition > 20) {
transform.Translate(Vector3.right * 15 * Time.deltaTime);
// }
}
}
}
}