Search Unity

Unity AR Foundation: Raycast to instantiated objects

Discussion in 'AR/VR (XR) Discussion' started by Kalisser, Apr 8, 2019.

  1. Kalisser

    Kalisser

    Joined:
    Jan 9, 2019
    Posts:
    12
    Hi,
    I did the AR Foundation tutorial


    Instead of the plane I have a small village and now I would like to click on the individual houses with a Raycast to get the names of it.
    Unfortunately this does not work for me. I have BoxCollider around my houses and Unity also has no error message.
    I also found this video

    and rebuilt it with Vuforia and it works here. Does this not work here with ARFoundation?

    This is the code:

    Code (CSharp):
    1.     private Text label;
    2.     private GameObject bg_label;
    3.  
    4.     // Use this for initialization
    5.     void Start()
    6.     {
    7.         label = GameObject.Find("LabelName").GetComponent<Text>();
    8.         label.text = "";
    9.  
    10.         bg_label = GameObject.Find("BgLabel");
    11.         bg_label.SetActive(false);
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.         if ((Input.GetTouch(0).phase == TouchPhase.Stationary) || (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).deltaPosition.magnitude < 1.2f))
    19.         {
    20.             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    21.             RaycastHit hit;
    22.             if (Physics.Raycast(ray, out hit))
    23.             {
    24.                 bg_label.SetActive(true);
    25.                 label.text = hit.transform.name.ToString();
    26.             }
    27.             else
    28.             {
    29.                 bg_label.SetActive(false);
    30.                 label.text = "";
    31.             }
    32.         }
    33.     }
    Thank you
    Lukas
     
  2. jordano00

    jordano00

    Joined:
    Jul 3, 2016
    Posts:
    8
    Same for me actually. What I know is that the touch is off by a lot. If you try to tap a GO, you have to tap it indirectly which sucks. Perhaps it has to do with the collider placements.