Rhythm Game - When I touch an object with mobile in only activates 80% of the time...

Hello, -

I am using android. -

I have a rhythm game where notes fall. This script it attached to the notes so that when you touch them on mobile they disappear. It works well but sometimes it doesn’t work… I touch them but they do not go to the blank sprite and I have no idea why…

I have 2 touches registered below as there is sometimes 2 that spawn at ones so you need to touch them both. Without the second section of code it would only let me press one at once.

void Update()

{

foreach (Touch touch in Input.touches)
    {
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
        touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
        RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);

        if (hitInformation.collider.tag != "Button")
        {
            touchedObject = hitInformation.transform.gameObject;
            touchedObject.GetComponent<SpriteRenderer>().sprite = blank;
        }
    }

    if (Input.touchCount > 0 && Input.GetTouch(1).phase == TouchPhase.Began)
    {
        touchPosWorld1 = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
        touchPosWorld2D2 = new Vector2(touchPosWorld1.x, touchPosWorld1.y);
        RaycastHit2D hitInformation2 = Physics2D.Raycast(touchPosWorld2D2, Camera.main.transform.forward);

        if (hitInformation2.collider.tag != "Button")
        {
            touchedObject1 = hitInformation2.transform.gameObject;
            touchedObject1.GetComponent<SpriteRenderer>().sprite = blank;
        }
    }
}    
}

I hope you can help.

Thank you very much.

Hello, You can try this :

 void Update()
        {
            if (Input.touchCount > 0)
            {
                for (int i = 0; i < Input.touchCount; i++)
                {
                    Touch touch = Input.GetTouch(i);
                    if (touch.phase == TouchPhase.Began)
                    {
                       Ray ray = Camera.main.ScreenPointToRay(touch.position);
                       RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction);
    
                        if (hitInformation.collider.tag != "Button")
                        {
                            GameObject touchedObject = hitInformation.transform.gameObject;
                            touchedObject.GetComponent<SpriteRenderer>().sprite = blank;
                            Instantiate(partRed, touchedObject.transform.position, Quaternion.identity);
                            partRed.Play();
                        }
                    }
                }
            }
    
        }

That will test all touches and make a ray directly by the camera.

Raph