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\$
3
  • \$\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
    Commented Sep 26, 2018 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
    Commented Sep 26, 2018 at 11:54
  • \$\begingroup\$ Feel free to add an answer showing what you did to fix it. \$\endgroup\$
    – DMGregory
    Commented Sep 26, 2018 at 11:56

1 Answer 1

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\$
1
  • \$\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
    Commented Sep 26, 2018 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .