saleheen
saleheen

Reputation: 11

Unity Touch Input

I have built a small game by unity. In which, when the player touch the screen a pin spawns. The problem I am having is, when I touch the screen, instead of one, two pin spawns at a time. I used this code-

 if(Input.touchCount == 1)
    {
        Spawnpin();
    }

Upvotes: 1

Views: 587

Answers (1)

Programmer
Programmer

Reputation: 125275

Put the code inside TouchPhase.Began or TouchPhase.Ended so that it will be called once only and will only be called again when the touch is released and pressed again. Deciding between TouchPhase.Began and TouchPhase.Ended depends on whether you want the touch to register immediately when it is pressed or after it is released.

void Update()
{
    for (int i = 0; i < Input.touchCount; i++)
    {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
        {   
            if (Input.touchCount == 1)
            {
                Spawnpin();
            }
        }
    }
}

Upvotes: 5

Related Questions