sooon
sooon

Reputation: 4878

Unity3d - Touch Input issue

I develop a game that need user to have a single touch to draw over to link multiple objects. Currently using mouse button as touch input with this code:

if(Input.GetButton("Fire1"))
    {
        mouseIsDown = true;
        ...//other codes
    }

It works fine with mouse but it will give me problem when I compile it for multi touch device. In multitouch device, if you have 2 fingers down, the middle point will be taken as the input. But if I already touch an object then with second finger down to screen, it will mess up my game input and give me havoc design.

What I want is to limit it to 1 touch only. If the second finger come touching, ignore it. How can I achieve that?

Upvotes: 1

Views: 1819

Answers (3)

JedH
JedH

Reputation: 124

I would roll with a bit of polling!

so in your Update() I typically do something like this:

foreach (Touch touch in Input.touches)
{
    // Get the touch id of the current touch if you're doing multi-touch.
    int pointerID = touch.fingerId;        

    if (touch.phase == TouchPhase.Began)
    {
        // Do something off of a touch.
    }
}

If you're looking for more info check this out: TouchPhases in Unity

Upvotes: -1

Programmer
Programmer

Reputation: 125245

Below is all you need:

     if ((Input.touchCount == 1) && (Input.GetTouch (0).phase == TouchPhase.Began))   {
            mouseIsDown = true;
            ...//other codes
     }

It will only fire when one finger is on the screen because of Input.touchCount == 1

Upvotes: 2

Atra Viator
Atra Viator

Reputation: 535

You are using Input.GetButton. A button is not a touch. You probably want to look at Input.GetTouch

If you need to support multiple input-type devices, you may want to consider scripting your own manager to abstract this out somewhat.

Upvotes: 0

Related Questions