Pookie
Pookie

Reputation: 1279

Updating of Touch Input in Unity not working

I am making a game like pong, except the user controls both paddles and is trying to keep the ball in bound. I am using touch to control both the paddles. The way I assigned the index of the touch is like so:

     public void checkTouchIndex() {
    if (Input.touchCount <= 1)
    {
        touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
        if (touchPos.x < 0)
        {
            Debugger.il = "Touch Index = 0";
            myTouch = Input.GetTouch(0);
        }

    }
    if (Input.touchCount >= 1)
    {
        touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
        if (touchPos.x < 0)
        {

            Debugger.il = "Touch Index = 1";
            myTouch = Input.GetTouch(1);

        }
    }
    else
        Debugger.il = "Side not touched";
}

Here is the code for moving the actual paddle:

     public void checkMovementTouch() {
    if (Input.touchCount > 0)
    {
        //assigns touch index
        checkTouchIndex();

        touchPos = Camera.main.ScreenToWorldPoint(myTouch.position);

        if (touchPos.x > 0)
        {

            //Begin Section: Touch left side
            if (touchPos.y > 0)
            {
                //moves upwards
                move(true);
            }
            else
            {
                //moves downwards
                move(false);

            }
            //End of section
        }

    }

}

Note: the Debugger is just showing text on screen of what side was touched and the x- position of the touches. However I am having difficulties updating. You should be able to drag your finger and have the paddle follow it, but after some testing I noticed that only one of the paddles is updating correctly to follow the finger, and it is always the paddle with the index of one.

For example, if I touch the ride side, then lay another finger on the left side. I will be able to drag my left side up and down correctly, but the right side, even when I move it up and down, will stay put.

Question: How can I get both the paddles to update perfectly.

Upvotes: 0

Views: 2494

Answers (2)

Allen
Allen

Reputation: 567

Jordan's answer gets you part of the way there, but doesn't explain how to get which touch belongs to which paddle.

Currently, since you have the same code on both paddles, both paddles will have the same touch assigned to them.

You need something that assigns the first touch on the paddles side to it.

I added a bit of pseudo-code to Jordans answer, for you to figure out the exact implementation.

if (Input.touchCount > 0)
{
    for (int i = 0; i < Input.touchCount; i++) 
    {
        touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(i));

        // if(touchPos.x is on my side)
        // {
        //    myTouch = touchPos;
        // }

    }
}

The best way would be to assign the touch upon the touch first being registered, and not looping through this code on each frame. Good luck with your project.

Upvotes: 1

Jordan Marx
Jordan Marx

Reputation: 56

I believe you should have a for loop go through all the touches. Here is some documentation on Input.GetTouch.

void Update() 
{

    if (Input.touchCount > 0)
    {
        for (int i = 0; i < Input.touchCount; i++) 
        {
            myTouch = Input.GetTouch(i);

            // ...
        }
    }
}

Upvotes: 2

Related Questions