Search Unity

Touching 2D Sprite not working on Mobile, but working on PC?

Discussion in '2D' started by Clayyface, Jan 4, 2021.

  1. Clayyface

    Clayyface

    Joined:
    May 8, 2019
    Posts:
    1
    Hey guys,
    So I followed a post on here for touching a 2D sprite, and it seems like it would work, however, when I run it on my phone, no touches are registered.

    If I replace the first line with "if (Input.GetMouseButtonDown(0))", then it works, but with the touch line instead of the mouse line, it doesn't work. Any suggestions? (Sorry for the long code block.)

    Code (CSharp):
    1. void Update()
    2.     {
    3.        
    4.        
    5.         if(Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    6.         {
    7.             Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
    8.             RaycastHit hit;
    9.    
    10.             if(Physics.Raycast(ray, out hit))
    11.             {
    12.                 if(hit.collider != null)
    13.                 {
    14.                                         if(hit.collider.gameObject.CompareTag("green"))
    15.                         {
    16.                             Destroy(hit.collider.gameObject);
    17.                             StartCoroutine(cameraShake.Shake(.15f, .15f));
    18.                             print("GREEN");
    19.                             score += 1;
    20.                             happyHit.Play();
    21.                         }
    22.                        
    23.                         if(hit.collider.gameObject.CompareTag("red"))
    24.                         {
    25.                             Vector2 newPos = new Vector2(lava.transform.position.x, lava.transform.position.y + .3f);
    26.                             lava.transform.position = newPos;
    27.                             StartCoroutine(cameraShake.Shake(.1f, .2f));
    28.                             print("RED");
    29.                             sadHit.Play();
    30.                         }
    31.  
    32.                         if(hit.collider.gameObject.CompareTag("lava"))
    33.                         {
    34.                             Vector2 newPos = new Vector2(lava.transform.position.x, lava.transform.position.y + .3f);
    35.                             lava.transform.position = newPos;
    36.                             StartCoroutine(cameraShake.Shake(.1f, .2f));
    37.                             sadHit.Play();
    38.                         }
    39.  
    40.                         if(hit.collider.gameObject.CompareTag("yellow"))
    41.                         {
    42.                             var yelRand = Random.Range(0,2);
    43.                             if(yelRand < 1)
    44.                             {
    45.                                 hit.collider.gameObject.tag = "red";
    46.                                 hit.collider.gameObject.GetComponent<SpriteRenderer>().material.color = redCol;
    47.                             }
    48.                             else
    49.                             {
    50.                                 hit.collider.gameObject.tag = "green";
    51.                                 hit.collider.gameObject.GetComponent<SpriteRenderer>().material.color = greenCol;
    52.                             }
    53.                         }
    54.  
    55.                         } else {
    56.                             Vector2 newPos = new Vector2(lava.transform.position.x, lava.transform.position.y + .3f);
    57.                             lava.transform.position = newPos;
    58.                             StartCoroutine(cameraShake.Shake(.1f, .2f));
    59.                             sadHit.Play();
    60.                 }
    61.             }
    62.         }
    63. }