Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Raycast error within ARFoundation

Discussion in 'AR' started by Chimaruk, Jul 25, 2019.

  1. Chimaruk

    Chimaruk

    Joined:
    Jul 25, 2019
    Posts:
    2
    Hello there,
    I'm a total beginner of Unity and scripting at all. So please excuse me when the solution to this is very simple and I'm just too dumb to find it.

    I have an AR Project, where you can place one object and size, rotate, color it whatever you like. Just a little gimmick, but a big problem here for me.
    I was following this tutorial:



    I wrote the code on PlaceOnPlane.cs, expecting it to work. But it just spits out two errors. Everything else works, but not this.
    I'm using Unity 2019.1.9f1 Personal.


    Errors:
    Error CS0234: The type or namespace name 'PlaneWithinPolygon' does not exist in the namespace 'UnityEngine.Experimental.XR' (are you missing an assembly reference?) (CS0234) (Assembly-CSharp)

    Error CS1061: 'ARSessionOrigin' does not contain a definition for 'Raycast' and no accessible extension method 'Raycast' accepting a first argument of type 'ARSessionOrigin' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp)

    Here's my code where the error is:

    Code (CSharp):
    1.  void Update()
    2.     {
    3.         if (Input.touchCount == 0)
    4.             return;
    5.  
    6.         Touch touch = Input.GetTouch(0);
    7. // errors right here
    8.         if (sessionOrigin.Raycast(touch.position, hits, UnityEngine.Experimental.XR.PlaneWithinPolygon) && IsPointerOverUIObject(touch.position))
    9.         {
    10.             Pose pose = hits[0].pose;
    11.  
    12.             if(!model.activeInHierarchy)
    13.             {
    14.                 model.SetActive(true);
    15.                 model.transform.position = pose.position;
    16.                 model.transform.rotation = pose.rotation;
    17.             }
    18.             else
    19.             {
    20.                 model.transform.position = pose.forward;
    21.             }
    22.         }
    23.     }
    So, I hope someone could help me with this. Thanks!
     
  2. lloydhooson

    lloydhooson

    Joined:
    Apr 7, 2008
    Posts:
    64
    Hi, from what I've seen (sorry havent read it all your post and none of that video), but in your code, you need to have something like this :

    Code (CSharp):
    1.  
    2.  
    3. protected ARRaycastManager m_RaycastManager;
    4.  
    5.     void Awake()
    6.     {
    7.         m_RaycastManager = GetComponent<ARRaycastManager>();
    8.     }
    9.  
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.touchCount == 0)
    14.             return;
    15.  
    16.         Touch touch = Input.GetTouch(0);
    17.         // errors right here
    18.         if (m_RaycastManager.Raycast(touch.position, hits, UnityEngine.Experimental.XR.PlaneWithinPolygon) && IsPointerOverUIObject(touch.position))
    19.         {
    20.             Pose pose = hits[0].pose;
    21.  
    22.             if (!model.activeInHierarchy)
    23.             {
    24.                 model.SetActive(true);
    25.                 model.transform.position = pose.position;
    26.                 model.transform.rotation = pose.rotation;
    27.             }
    28.             else
    29.             {
    30.                 model.transform.position = pose.forward;
    31.             }
    32.         }
    33.     }

    You need to have a ARRaycastManager component on the AR session origin, and then use that for your ray casting.

    hope this gets you closer. plus never tested that code, but note-padded it.
     
    Chimaruk likes this.
  3. Chimaruk

    Chimaruk

    Joined:
    Jul 25, 2019
    Posts:
    2
    @lloydhooson Alright, thanks, this helped a lot!
    Now there's only one error (PlaneWithinPolygon) left.
     
  4. lloydhooson

    lloydhooson

    Joined:
    Apr 7, 2008
    Posts:
    64
    check out >

    UnityEngine.XR.ARSubsystems.PlaneWithinPolygon