0
\$\begingroup\$

I'm trying to create a Character touch controls for 2d platform.

In my script Im using one horizontal swipe and hold touch gesture and one vertical swipe gesture without hold.Both are different methods called in void Update ().

And here is my script :

void Update () 
    {

        HorizontalSwipe();
        VerticalSwipe ();


    }


    void HorizontalSwipe()                      
    {
        foreach (Touch FingerTouchx in Input.touches)
        {
            if(FingerTouchx.fingerId<1)//         FINGERID

            {
                //if(FingerTouchx.position.x < Screen.width/2)

                if(FingerTouchx.phase == TouchPhase.Began)
                {
                    FingerInitialPositionx=FingerTouchx.position.x;

                }


                else if(FingerTouchx.phase == TouchPhase.Moved)
                {
                    FingerMovedPositionx=FingerTouchx.position.x;


                    if(FingerMovedPositionx>FingerInitialPositionx)
                    {
                        charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
                    }


                    else if(FingerMovedPositionx<FingerInitialPositionx)
                    {
                        charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
                    }


                }


                else if(FingerTouchx.phase == TouchPhase.Ended)
                {
                    FingerInitialPositionx=0f;
                    FingerMovedPositionx=0f;

                }


                else 
                {

                    FingerMovedPositionx=FingerTouchx.position.x;

                    if(FingerMovedPositionx>FingerInitialPositionx)
                    {
                        charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
                    }


                    else if(FingerMovedPositionx<FingerInitialPositionx)
                    {
                        charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);

                    }


                }


            }

        }
    }



    void VerticalSwipe()
    {
        foreach (Touch FingerTouchy in Input.touches)
        {



            if(FingerTouchy.fingerId<1)      //         FINGERID


            {
                //if(FingerTouchy.position.y > Screen.width/2)

                if(FingerTouchy.phase == TouchPhase.Began)
                {
                    FingerInitialPositiony=FingerTouchy.position.y;


                }
                if(FingerTouchy.phase == TouchPhase.Moved)
                {
                    FingerMovedPositiony=FingerTouchy.position.y;

                }
                if(FingerTouchy.phase == TouchPhase.Ended)
                {
                    if(FingerMovedPositiony>FingerInitialPositiony)
                    {
                        charcter.transform.Translate(Vector2.up * speed * Time.deltaTime);
                    }
                    if(FingerMovedPositiony<FingerInitialPositiony)
                    {
                        charcter.transform.Translate(-Vector2.up * speed * Time.deltaTime);
                    }
                    FingerInitialPositiony=0f;
                    FingerMovedPositiony=0f;

                }

                else
                {}


            }
        }
    }

Problem:

1:When i swipe and hold horizontally the character moves fine but when i take my finger back it triggers the vertical swipe method.

2:When i try to swipe only vertically the horizontal swipe is also triggered.

3: The two swipe gestures are overlapping and triggered at same time on a Single touch.

Solution(Guess):Dividing the touch screen equally half and forcing horizontal touch gestures strictly on left side of touch screen and vertical touch gestures on right side.

But I'm beginner(You can see my coding style) and would like to know how to do it programatically. Im still learning.

\$\endgroup\$
  • \$\begingroup\$ Seems like a duplicate of your question gamedev.stackexchange.com/questions/113057/… - please don't repost the same question - instead edit the existing one, or add comments explaining what else you have tied. \$\endgroup\$ – Steven Dec 18 '15 at 7:18
  • \$\begingroup\$ @Steven answers for that question didn't help much \$\endgroup\$ – user75648 Dec 18 '15 at 7:20
  • \$\begingroup\$ How did you get that vertical method is executing? Which block of code in Vertical method is executing? What is happening? \$\endgroup\$ – Hamza Hasan Dec 18 '15 at 7:28
  • \$\begingroup\$ @HamzaHasan don't know, you are free to test this script if you want. \$\endgroup\$ – user75648 Dec 18 '15 at 7:32
  • \$\begingroup\$ Mark break points on each check of vertical code block then you will get know \$\endgroup\$ – Hamza Hasan Dec 18 '15 at 7:35
1
\$\begingroup\$

You actually want to split screen into two to get gesture Horizontal and Vertical. So use Screen.width for this purpose. You are checking position.y in vertical, which is not correct. You have check X in either case. Here is modified code with 2 touches.

void Update () 
{
    HorizontalSwipe();
    VerticalSwipe ();
}

void HorizontalSwipe()                      
{
    foreach (Touch FingerTouchx in Input.touches)
    {
        if(FingerTouchx.fingerId<2)//         FINGERID

        {
            if(FingerTouchx.position.x > Screen.width/2)
            continue;

            if(FingerTouchx.phase == TouchPhase.Began)
            {
                FingerInitialPositionx=FingerTouchx.position.x;
            }

            else if(FingerTouchx.phase == TouchPhase.Moved)
            {
                FingerMovedPositionx=FingerTouchx.position.x;

                if(FingerMovedPositionx>FingerInitialPositionx)
                {
                    charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
                }

                else if(FingerMovedPositionx<FingerInitialPositionx)
                {
                    charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
                }
            }

            else if(FingerTouchx.phase == TouchPhase.Ended)
            {
                FingerInitialPositionx=0f;
                FingerMovedPositionx=0f;
            }

            else 
            {
                FingerMovedPositionx=FingerTouchx.position.x;

                if(FingerMovedPositionx>FingerInitialPositionx)
                {
                    charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
                }

                else if(FingerMovedPositionx<FingerInitialPositionx)
                {
                    charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);

                }
            }
        }
    }
}



void VerticalSwipe()
{
    foreach (Touch FingerTouchy in Input.touches)
    {
        if(FingerTouchy.fingerId<2)      //         FINGERID

        {
            if(FingerTouchy.position.x < Screen.width/2)
            continue;

            if(FingerTouchy.phase == TouchPhase.Began)
            {
                FingerInitialPositiony=FingerTouchy.position.y;
            }

            if(FingerTouchy.phase == TouchPhase.Moved)
            {
                FingerMovedPositiony=FingerTouchy.position.y;
            }

            if(FingerTouchy.phase == TouchPhase.Ended)
            {
                if(FingerMovedPositiony>FingerInitialPositiony)
                {
                    charcter.transform.Translate(Vector2.up * speed * Time.deltaTime);
                }
                if(FingerMovedPositiony<FingerInitialPositiony)
                {
                    charcter.transform.Translate(-Vector2.up * speed * Time.deltaTime);
                }
                FingerInitialPositiony=0f;
                FingerMovedPositiony=0f;
            }

            else
            {}
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Tested your code.When i swipe up the horizontal swipe is trigged, vertical swipe is completely absent or ignored. \$\endgroup\$ – user75648 Dec 18 '15 at 7:57
  • \$\begingroup\$ Yea, I did that. What else you want then? \$\endgroup\$ – Hamza Hasan Dec 18 '15 at 7:58
  • \$\begingroup\$ the vertical and horizontal swipe must be handled at same time or at different time but should not overlap like in this youtube.com/watch?v=djVPd2pO3Co \$\endgroup\$ – user75648 Dec 18 '15 at 8:00
  • \$\begingroup\$ Split screen in two? one for horizontal and other for vertical? \$\endgroup\$ – Hamza Hasan Dec 18 '15 at 8:02
  • \$\begingroup\$ i used if(FingerTouchx.position.x < Screen.width/2) in void HorizontalSwipe() works fine for first time and from second time horizontal swipe is ignored and whole screen accepts vertical swipe only \$\endgroup\$ – user75648 Dec 18 '15 at 8:09

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.