- Home /
Mobile swipes causing player to move different distances randomly
The player will move (teleport) a set distance left or right when the user swipes left or right, but for some reason the distance the player moves keeps changing. I was testing on my iPhone 5
public float distance = 12f;
public float distancego = 12f;
rb2D = GetComponent<Rigidbody2D>();
void Update () {
rb2D.MovePosition(rb2D.position + velocity * Time.fixedDeltaTime);
Vector2 direction = new Vector2(5f, 1f);
Vector2 direction1 = new Vector2(-5f, -1f);
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{//right swipe
rb2D.MovePosition(rb2D.position + direction * distancego);
}
else if (swipeValue < 0)
{
//left swipe
rb2D.MovePosition(rb2D.position + direction1 * distance);
}
}
break;
}
}
Comment
Your answer
Follow this Question
Related Questions
Activity Indicator Centered 0 Answers
Saving Data on platform cloud 0 Answers
Unity IAP Services not Working on iOS. Purchase Dialogue does not Show at All 6 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers