# Multi-touch problem in a 2 player pong game

Hello, I’m trying to make a 2 player pong game for android and I have some problems with the multi touch:
Lets say I am player 1, and my brother is player 2,if my brother removes his finger from screen, and I keep my finger on screen, his paddle will continue to go up/ or down till I remove my finger.
That won’t happen if I am the only one using the touchscreen, it only happenes with both of us use it

Here is the code from player one:

``````var speed : float = 10;

function Update () {

if (Input.touchCount > 0)
{
var touchDeltaPos:Vector2 = Input.GetTouch(0).position;
if(Input.touchCount>1)
var touchDeltaPos2:Vector2 = Input.GetTouch(1).position;
if(touchDeltaPos.x<Screen.width/2)
{
if(touchDeltaPos.y > Screen.height/2)
{
rigidbody2D.velocity.y = 1*speed;
}
else rigidbody2D.velocity.y = -1*speed;
}
else if(touchDeltaPos2.x<Screen.width/2&&Input.touchCount>1)
{
if(touchDeltaPos2.y > Screen.height/2)
{
rigidbody2D.velocity.y = 1*speed;
}
else rigidbody2D.velocity.y = -1*speed;
}
}
if (Input.touchCount == 0)
rigidbody2D.velocity.y = 0;
rigidbody2D.velocity.x=0;
}
``````

here is the code from player 2:

``````var speed : float = 10;

function Update () {

if (Input.touchCount > 0)
{
var touchDeltaPos:Vector2 = Input.GetTouch(0).position;
if(Input.touchCount>1)
var touchDeltaPos2:Vector2 = Input.GetTouch(1).position;
if(touchDeltaPos.x>Screen.width/2)
{
if(touchDeltaPos.y > Screen.height/2)
{
rigidbody2D.velocity.y = 1*speed;
}
else rigidbody2D.velocity.y = -1*speed;
}
else if(touchDeltaPos2.x > Screen.width/2&&Input.touchCount>1)
{
if(touchDeltaPos2.y > Screen.height/2)
{
rigidbody2D.velocity.y = 1*speed;
}
else rigidbody2D.velocity.y = -1*speed;
}
}
if (Input.touchCount == 0)
rigidbody2D.velocity.y = 0;
rigidbody2D.velocity.x=0;
}
``````

In both scripts you are resetting the speed.y only IF touch count is 0. You should do the reset IF there are no touches or if neither of the touches happen on that player’s side of the screen

Here’s my solution in C#. I use this script for both players, and just check to see if the touch is close to the player’s Y axis. If so, I assume the touch must be intended for that user.

``````void Update () {
if (Input.touchCount > 2) {
return;		// dashing the dreams of four player mode
}

foreach (Touch touch in Input.touches)
{
Vector3 touchPos =  Camera.main.ScreenToWorldPoint (touch.position);

Vector2 myPostion = gameObject.GetComponent<Rigidbody2D> ().position;

// if touch is within 2 units of my vertical axis
if(Mathf.Abs(touchPos.x - myPostion.x) <= 2) {
myPostion.y = Mathf.Lerp (myPostion.y, touchPos.y, (speed + speedIncrease));
myPostion.y = Mathf.Clamp (myPostion.y, -14, 14);	// stay between -14 and 14 (screen height; YMMV)
gameObject.GetComponent<Rigidbody2D> ().position  = myPostion;
}
}
}
``````