0
\$\begingroup\$

Heres my PlayerControl script

void Update () {

    foreach (Touch FingerTouch in Input.touches)
    {
        if(FingerTouch.phase == TouchPhase.Began)
        {
            FingerInitialPosition=FingerTouch.position.x;
       }
        if(FingerTouch.phase == TouchPhase.Moved)
        {
            FingerMovedPosition=FingerTouch.position.x;
            if(FingerMovedPosition>FingerInitialPosition)
            {
                charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
           }
            if(FingerMovedPosition<FingerInitialPosition)
            {
                charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
            }
            }
        if(FingerTouch.phase == TouchPhase.Ended)
        {
            FingerInitialPosition=0f;
            FingerMovedPosition=0f;

        }


    }


}

}

The problem here is the player doesn't move when you swipe and hold on the touch screen but only moves when you keep swiping.

Can you please help whats wrong or missing in the code so that the player moves when swiped and held?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It only works when you keep swiping because you only update the player's position during the TouchPhase.Moved event. If you want to update the player's position even when the user's finger hasn't been moving, you need a default case:

void Update () {

    foreach (Touch FingerTouch in Input.touches)
    {
        if(FingerTouch.phase == TouchPhase.Began)
        {
            FingerInitialPosition=FingerTouch.position.x;
        }
        else if(FingerTouch.phase == TouchPhase.Moved)
        {
            FingerMovedPosition=FingerTouch.position.x;
            if(FingerMovedPosition>FingerInitialPosition)
            {
                charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
            }
            else if(FingerMovedPosition<FingerInitialPosition)
            {
                charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
            }
        }
        else if(FingerTouch.phase == TouchPhase.Ended)
        {
            FingerInitialPosition=0f;
            FingerMovedPosition=0f;

        }
        else //NEW PART!//
        {
            FingerMovedPosition=FingerTouch.position.x;
            if(FingerMovedPosition>FingerInitialPosition)
            {
                charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
            }
            else if(FingerMovedPosition<FingerInitialPosition)
            {
                charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);

            }
        }
    }
}

In addition to the new statement, the others are all else if as well.

\$\endgroup\$
3
  • \$\begingroup\$ How do i disable multiple horizontal swipe? \$\endgroup\$ Commented Dec 14, 2015 at 16:11
  • \$\begingroup\$ You should store the touch-ID of the tracked finger and compare. \$\endgroup\$ Commented Dec 14, 2015 at 16:23
  • \$\begingroup\$ please have a look here gamedev.stackexchange.com/q/113057/75648 \$\endgroup\$ Commented Dec 15, 2015 at 16:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.