0
\$\begingroup\$

My Code is as follows:

 foreach (Touch touch in Input.touches) {

        switch (touch.phase) {

        case TouchPhase.Began:

            touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            offset = touchPosition - GOCenter;

            break;
        case TouchPhase.Moved:
            touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

            newGOCenter = touchPosition - offset;
            newGOCenter.y = 0.0f;
            newGOCenter.z = 0.0f;
            gameObject.transform.position = newGOCenter;

            gameObject.transform.position = new Vector3 (
                Mathf.Clamp (gameObject.transform.position.x, -boundary.xMinMax, boundary.xMinMax),
                0.0f,
                0.0f
            );  

            break;
        default:

            break;
        }
    }

What i am doing is i am moving the player left and right along x-axis by dragging in the screen.

When i drag/move the player with one finger then it works as expected.

But when i keep another finger in the screen and take out the first finger from the screen the player comes of the second finger.

I want the player to be moved from the place where it was left instead of coming to the second finger.

[I have done] - disabled multiTouch but it does not worked. - user fingerIndex

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Try checking the touch.fingerId before moving your game object so that you can ensure you are only updating the position based on the first touch.

The reason you are seeing the behaviour that you are seeing is because you're updating the position for every single touch in the Input.touches array since you're continuing to iterate through the array after you have already updated the position. Checking the fingerId beforehand and only updating the position when it is the fingerId of your first touch should prevent this from occurring.

Also, I'd highly recommend using touch.position rather than Input.mousePosition as it is far more accurate; when more than one finger is being used Input.mousePosition will return the average of all touches so you won't be getting an accurate position.

\$\endgroup\$
0

You must log in to answer this question.

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