Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mobile touch for my rhythm game not 100% working. Not all note are touchable..

Discussion in 'Editor & General Support' started by Russellcart, Jan 6, 2019.

  1. Russellcart

    Russellcart

    Joined:
    Dec 22, 2018
    Posts:
    2
    Good day,

    I am in the process of making a rhythm game. My first time trying touch capability in one of my games. Basically when the 'notes' spawn and I touch them some of them don't register as hit even though I 100% hit them.

    I tired counting to see if it every x note didn't work but that wasn't the case.

    Code (CSharp):
    1.   void Update()
    2.      {
    3.          foreach (Touch touch in Input.touches)
    4.          {
    5.              if (Input.touchCount >= 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    6.              {
    7.                  touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    8.                  Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    9.                  RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    10.                  if (hitInformation.collider.tag != "Button")
    11.                  {
    12.                      GameObject touchedObject = hitInformation.transform.gameObject;
    13.                      touchedObject.GetComponent<SpriteRenderer>().sprite = blank;
    14.                      Instantiate(partRed, touchedObject.transform.position, Quaternion.identity);
    15.                      partRed.Play();
    16.                  }
    17.                  }
    18.              }
    19.  
    20. **// 2nd touch..**
    21.          if (Input.touchCount > 0 && Input.GetTouch(1).phase == TouchPhase.Began)
    22.          {
    23.              touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
    24.              Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    25.              RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    26.              if (hitInformation.collider.tag != "Button")
    27.              {
    28.                  GameObject touchedObject1 = hitInformation.transform.gameObject;
    29.                  touchedObject1.GetComponent<SpriteRenderer>().sprite = blank;
    30.                  Instantiate(partRed, touchedObject1.transform.position, Quaternion.identity);
    31.                  partRed.Play();
    32.              }
    33.              }
    34.  
    35. **// 3rd touch..**
    36.          if (Input.touchCount > 0 && Input.GetTouch(2).phase == TouchPhase.Began)
    37.          {
    38.              touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
    39.              Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    40.              RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    41.              if (hitInformation.collider.tag != "Button")
    42.              {
    43.                  GameObject touchedObject2 = hitInformation.transform.gameObject;
    44.                  touchedObject2.GetComponent<SpriteRenderer>().sprite = blank;
    45.                  Instantiate(partRed, touchedObject2.transform.position, Quaternion.identity);
    46.                  partRed.Play();
    47.              }
    48.          }
    49.      }

    The particles don't won't either but I posted that into another question on this board ;).

    Thanks for your help.

    Russell
     
  2. RadonRaph

    RadonRaph

    Joined:
    Jun 12, 2016
    Posts:
    3
    Maybe you can try this:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.touchCount > 0)
    4.         {
    5.             for (int i = 0; i < Input.touchCount; i++)
    6.             {
    7.                 Touch touch = Input.GetTouch(i);
    8.                 if (touch.phase == TouchPhase.Began)
    9.                 {
    10.                    Ray ray = Camera.main.ScreenPointToRay(touch.position);
    11.                    RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction);
    12.  
    13.                     if (hitInformation.collider.tag != "Button")
    14.                     {
    15.                         GameObject touchedObject = hitInformation.transform.gameObject;
    16.                         touchedObject.GetComponent<SpriteRenderer>().sprite = blank;
    17.                         Instantiate(partRed, touchedObject.transform.position, Quaternion.identity);
    18.                         partRed.Play();
    19.                     }
    20.                 }
    21.             }
    22.         }
    23.  
    24.     }
    That will test all touches and make a ray directly by the camera.

    Raph