Search Unity

What is difference between XRRaycastHit and XRRaycast?

Discussion in 'AR' started by hmkn, Jun 9, 2020.

  1. hmkn

    hmkn

    Joined:
    Nov 7, 2019
    Posts:
    23
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,142
    XRRaycast is a tracked raycast that updates over time. Calling ARRaycastManager.AddRaycast() will return
    ARRaycast (which is a managed wrapper for XRRaycast). Then use can subscribe to its
    Action<ARRaycastUpdatedEventArgs> updated
    event to listen when XRRaycast updates its precision.

    XRRaycastHit on the other hand, is a data holder that contains information about raycast hit. You get it from calling ARPlaneManager.Raycast() and ARPointCloudManager.Raycast().

    For most of usages ARPlaneManager.Raycast() will be enough. But if you're looking for the best precision, then look for ARRaycastManager.AddRaycast().
     
  3. hmkn

    hmkn

    Joined:
    Nov 7, 2019
    Posts:
    23
    Thank you for the reply.
    So when I use ARRaycastManager.AddRaycast(), ARRaycastManager instantiates prefab that GameObject has ARRaycast component on it like ARPlaneManager. Is that correct?
     
  4. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,142
    I experimented with it yesterday, and its behavior is not very obvious.
    Yes, ARRaycastManager will spawn prefab with ARRaycast component on the intersection between ray coming from screen position and detected plane. Then ARRaycast will update its position automatically when a new more precise position is received.
     
  5. hmkn

    hmkn

    Joined:
    Nov 7, 2019
    Posts:
    23
    Thank you for your experiment.
     
  6. hmkn

    hmkn

    Joined:
    Nov 7, 2019
    Posts:
    23
    I've got raycast component by calling AddRaycast like this.
    ARRaycast raycast = raycastManager.AddRaycast(touchPosition, 5);
    Then I'm trying to get position of the object. How should I get it?
     
  7. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,142
    ARRaycast is a MonoBehavior so you can get its position by raycast.transform.position. But check for null first.
     
  8. hmkn

    hmkn

    Joined:
    Nov 7, 2019
    Posts:
    23
    Many thanks. I got it.