- Home /
1 finger object dragging ignoring the second one
I'm trying to make a ball draggable around the screen without the need to touch it when dragging. I mean if I tap an empty space and drag, the ball should move the samed "dragged" distance.
I've managed to make it work if I only use one finger. The problem is that when the user uses another finger the ball goes crazy.
The working code for "one finger" I'm using is: (where inicial means Initial)
if(Input.GetMouseButton(0) && playing)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, rayCastHit))
{
xfinal = rayCastHit.point.x;
yfinal = rayCastHit.point.y;
if(!isJump){
transform.position.x = transform.position.x + (xfinal - xinicial)*sensibility; //OJJOOO falla aqui!!
transform.position.y = transform.position.y + (yfinal - yinicial)*sensibility;
}
xinicial = xfinal;
yinicial = yfinal;
if(isJump) Debug.Log("is Jumping");
isJump = false;
if(!isJump) Debug.Log("Nojump");
}
}
else{
isJump = true;
Debug.Log("is Jump = " + isJump.ToString());
}
I undersand that when the second finger touches the screen this line generates the problem:
transform.position.y = transform.position.y + (yfinal - yinicial);
Any suggestion how to manage the second finger ?
There's no finger dragging code here, but one solution is to limit your processing to when there has only been a single finger during that touch 'session'. That is when the first finger goes down, you enable dragging. When a second finger goes down you disable dragging until there are no fingers touching again.
Where is the code for touchscreen control? You can use Input.GetTouch() to get the finger ID. Input.GetTouch(0) is the first finger that touched the screen, Input.GetTouch(1), if there are 2 fingers touching then 1 is the ID of the second finger and so on. You can use the ID t get the position and state of either finger
Your answer