Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Android ArFoundation, issues with sensing touch on game object.

Discussion in 'AR' started by jbuts, Nov 20, 2022.

  1. jbuts

    jbuts

    Joined:
    Feb 9, 2022
    Posts:
    4
    I have a game object (cube) that when clicked I want to open a web URL. Here is the code I am running:


    Code (CSharp):
    1.  
    2. string btnName;
    3.  
    4. void Update()
    5.     {
    6.         if (Input.touchCount > 1 && Input.touches[0].phase == TouchPhase.Began)
    7.         {
    8.             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    9.             RaycastHit Hits;
    10.             if (Physics.Raycast(ray, out Hits))
    11.             {
    12.                 btnName = Hits.transform.name;
    13.                 switch (btnName)
    14.                 {
    15.                     case "DresserShopButton":
    16.                         Application.OpenURL("https://google.com");
    17.                         break;
    18.                     default:
    19.                         break;
    20.                 }
    21.  
    22.             }
    23.         }
    24.     }
    I tried running this script in the interaction controller to see if it would work in either. It did not work.

    The idea is that I will have a list of cubes that when clicked will go to a unique URL. For now I only have one cube I am trying to work with.
    Here is how the cube is set up:
    upload_2022-11-20_9-21-11.png
    I have tried with mesh collider enable as well, but it did nothing to help and to my understanding a mesh collider would just slow the app down.

    I am able to open the URL using a button and evoking a function in a script on the buttons click, however a button will not work for what I am trying to do as I need a cube to be clicked that is fixed to a parent game object (the cube is in a container and is associated to a 3d model).
    upload_2022-11-20_9-24-57.png

    Any advice helps.
     

    Attached Files:

  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,025
    When you say your code didn't work, what specifically do you mean? There are several points where this could fail:

    • Input was not detected?
    • Ray was cast but not with the correct origin and direction?
    • Ray was cast with the correct origin and direction, but did not register a collision?
    • Ray was cast with the correct origin and direction, but registered a collision with a GameObject of the wrong name?

    In particular when looking at your code, why do you require that
    Input.touchCount > 1
    ? If there is exactly one touch on the screen, do you intend for this not to trigger?
     
  3. jbuts

    jbuts

    Joined:
    Feb 9, 2022
    Posts:
    4
    Ohh my goodness. I forgot it should be >=1. Wow. Thanks.
     
    andyb-unity likes this.