Search Unity

Tap to Jump & Swipe to Attack Mobile Touch Input Issue

Discussion in 'Scripting' started by Viridianite, Jun 17, 2019.

  1. Viridianite

    Viridianite

    Joined:
    Oct 24, 2018
    Posts:
    1
    I am working on a simple mobile platformer project. In one of my scenes the player gets the ability to attack, and before this the only input is a jump. Before getting the attack the jump works fine.

    In my script I have it set up to jump when the screen is tapped, and to attack on a swipe right. They both work, when I swipe right the player will only attack as expected, however when I tap to jump the player will jump AND attack at the same time. I have tried several different things and I cannot figure out why it's doing this.

    I found a simple touch input script from here:
    https://forum.unity.com/threads/simple-swipe-and-tap-mobile-input.376160/

    and I've tweaked it a bit to try and get this working the way I need:


    if (Input.touchCount > 0)
    {
    tap = Input.GetTouch(0);

    if (tap.phase == TouchPhase.Began)
    {
    startTouch = tap.position;
    endTouch = tap.position;
    }

    if (tap.phase == TouchPhase.Moved)
    {
    currentTouch = tap.position;
    }

    if (tap.phase == TouchPhase.Ended)
    {
    endTouch = tap.position;

    if ((Mathf.Abs(endTouch.x - startTouch.x) > swipeDist || Mathf.Abs(endTouch.y - startTouch.y) > swipeDist) || (Mathf.Abs(startTouch.x - endTouch.x) > swipeDist || Mathf.Abs(startTouch.y - endTouch.y) > swipeDist)) //Drag
    {
    if (Mathf.Abs(endTouch.y - startTouch.y) > Mathf.Abs(endTouch.x - startTouch.x)) //Vertical
    {
    if (endTouch.y > startTouch.y) //Up
    {

    }
    else //Down
    {

    }
    }
    else //Horizontal
    {
    if (endTouch.x > startTouch.x) //Right
    {
    // Attack function
    }
    else //Left
    {

    }
    }

    }
    else if (Mathf.Abs(endTouch.x - startTouch.x) < swipeDist && Mathf.Abs(endTouch.y - startTouch.y) < swipeDist) //Tap
    {
    // Jump function
    }

    }

    }



    but no matter what I try the jump will always play an attack as well. Any ideas how I can fix this? TIA