0
\$\begingroup\$

I currently have a working controller with left and right but I want to set the velocity of the character to 0/Stop when 2 or more touches are detected. This is my current working code but I want to make the player stop when 2 touches are detected.

void TouchMove(){

    if(Input.touchCount > 0){

        Touch touch = Input.GetTouch(0);

        float middle = Screen.width / 2;

        if(touch.position.x < middle && touch.phase == TouchPhase.Began) {
            MoveLeft();
        }
        else if (touch.position.x > middle && touch.phase == TouchPhase.Began) {
            MoveRight();
        }

    }
    else {
        SetVelocityZero();
    }
}`

This is how my game looks like enter image description here

\$\endgroup\$
  • \$\begingroup\$ It looks like you're already reading the touch count. Have you tried checking for if(Input.touchCount >= 2) { /* don't move */ } ? Show us what you tried, and describe what went wrong. \$\endgroup\$ – DMGregory Sep 26 '18 at 11:51
  • \$\begingroup\$ Oh I see tried that earlier but I guess I wrote if(Input.touches >= 2){ SetVelocityZero(); } Which is wrong \$\endgroup\$ – DeeMastah Sep 26 '18 at 11:54
  • \$\begingroup\$ Feel free to add an answer showing what you did to fix it. \$\endgroup\$ – DMGregory Sep 26 '18 at 11:56
1
\$\begingroup\$

The solution for this problem is by inserting this code in the touchmove function

if(Input.touchCount >= 2){ SetVelocityZero(); }

Thank you DMGregory for the answer!

\$\endgroup\$
  • \$\begingroup\$ You can also if(Input.touchCount == 1) { /* do movement */ } else { /* stop */ } to catch both the 0 touches and 2+ touches cases in one fell swoop. \$\endgroup\$ – DMGregory Sep 26 '18 at 13:55

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.