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
\$\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\$
  • \$\begingroup\$ How do i disable multiple horizontal swipe? \$\endgroup\$ – user75648 Dec 14 '15 at 16:11
  • \$\begingroup\$ You should store the touch-ID of the tracked finger and compare. \$\endgroup\$ – Draco18s Dec 14 '15 at 16:23
  • \$\begingroup\$ please have a look here gamedev.stackexchange.com/q/113057/75648 \$\endgroup\$ – user75648 Dec 15 '15 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.