Search Unity

Raycasting objects by its Tag with ARFoundation

Discussion in 'Handheld AR' started by tonOnWu, Nov 5, 2018.

  1. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
    Hi guys.

    I'm creating an AR game where I need to tap over a 3D Object on my scene an shoot to its position. That's the essentially my problem.

    Yes, as I understand from ARFoundation Documentation, you can only Raycast Planes. I mean, currently, you can't detect other 3D objects in the scene. Please, if I'm wrong, do you have an example in code of how to detect an object by its tag? as it's on Raycasting out of AR.

    Now I'm pretty sure that by now there must be some people requiring to make a Raycast over a 3D object (not a plane). Probably they bend the rules. Do you have an example of it that you may share? What I'm looking for is to Raycasting Detect an object by its tag using ARFoundation.

    A simple code of Raycasting over 3D is something like this:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         RaycastHit hit;
    4.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
    5.         {
    6.             Debug.Log("Raycasting Detected: " + hit.collider.tag);
    7.             if (hit.collider.tag == "DroneStoreItem")
    8.             {
    9.                 GameObject droneStoreItem = hit.collider.gameObject;
    10.  
    11.                 if (droneStoreItem.GetComponent<DroneStoreItem>() != null)
    12.                 {
    13.                     droneStoreItem.GetComponent<DroneStoreItem>().DroneStoreBoxTouch();
    14.                 }
    15.             }
    16.         }
    17.     }
    18.  
    Now, how can I translate previous code using ARSessionOrigin, Pose and a TrackableType (NOT a Plane or Feature Point).

    I'd really appreciate any help.
     
    Last edited: Nov 5, 2018
  2. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
  3. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
  4. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
  5. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
    Your question about why don't use Physics Raycasting in the AR, is really good. I haven't consider that. Let me try it.
     
  6. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
    THANK YOU greenmachine. Your suggestion as simple as it was, got right in the center. It worked perfectly. Sometimes we entangle ourselves in difficult solutions when the obvious answer is in front of you. Thank you for giving me a new and simple perspective.
     
  7. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    It sounds like you've found an answer that suits you, but I was curious about your question:
    The AR raycast only raycasts against things that have been detected by the device. Currently, handheld AR (ARCore & ARKit) only detect feature points (points in the point cloud) and planes. Are you trying to use the AR raycast as a general raycaster for (virtual) content in your scene?
     
  8. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
    Hi @tdmowrer. Yes, that was exactly what I was trying to do. I need to tap over virtual elements on an AR game.
     
  9. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Ah okay. Unfortunately, that isn't what the AR raycaster is for. Sounds like you've got a solution that works for you :)
     
  10. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
    Yes. I really think that ARFoundation is awesome, but it needs a lot more documentation and examples. We're working on several projects with it but we had spent a lot of time trying to understand simple things. I hope you have that in consideration for next upgrades.
    Thanks for your support.
     
  11. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    For what would you like to see more samples and documentation? Both manual and full scripting API documentation is here -- let me know what's missing :)
     
    tonOnWu likes this.
  12. tonOnWu

    tonOnWu

    Joined:
    Jun 23, 2017
    Posts:
    59
    When you launched the experimental-AR Interface, you created a lot of different cool examples. What I suggest is to have those same examples but for this official release.

    Thank you for the documentation. By now, I almost know it by memory. But nothing like an example.
     
  13. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    A couple of things that have come up often on this forum, and would benefit from an example for those new to ARFoundation:

    > Disabling plane detection and cleaning out detected planes after you have placed your content
    > FocusSquare/UI while scanning for planes

    In the absence of an ARFoundation editor toggle to turn on/off vertical plane detection
    > Example of how to ignore vertical planes
     
  14. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Thanks for the feedback. I've added two new scenes to the samples repo which demonstrate
    1. Toggle plane detection and existing planes
    2. Selectively disable vertical planes
    The FocusSquare UI is also a good suggestion. I'll look into that too.
     
  15. GreeneMachine

    GreeneMachine

    Joined:
    Jul 3, 2015
    Posts:
    126
    Great thanks.
    Let us know if/when you commit a FocusSquare sample.
     
    newguy123 likes this.
  16. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    166
    +1
    An example of this would be absolutely awesome
     
  17. Doomguy16

    Doomguy16

    Joined:
    Jul 30, 2019
    Posts:
    2
    Hello tonOnwu I really need to know the details how above solution worked for you?As I am new to unity and I am working on a project I need to submit really soon!!Thx for any help beforehand
     
  18. soorya696

    soorya696

    Joined:
    Dec 13, 2018
    Posts:
    24
  19. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Your code computes a Ray
    Code (csharp):
    1.  
    2. Ray ray = Camera.main.ScreenPointToRay(touch.position);
    3. raycastManager.Raycast(ray, hits, TrackableType.FeaturePoint);
    4.  
    which is okay, but unnecessary. The Raycast interface accepts screen points too.

    I'm not sure what you mean by "wrong results", but I noticed you aren't checking whether the raycast actually hits anything, so I imagine it's probably throwing an IndexOutOfRangeException when you unguardedly access
    hits[0]
    .

    Can you be more specific about what doesn't work?
     
  20. soorya696

    soorya696

    Joined:
    Dec 13, 2018
    Posts:
    24
    Thanks for the reply. I made it working. The issue is the feature point pose is in local space. I converted it into world space and everything works fine.
     
  21. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    547
    Hmm, that doesn't sound right. The ARRaycastHit.pose is in world space. There's a separate property for accessing the "local" (i.e., relative to the session origin) pose: ARRaycastHit.sessionRelativePose. Could you post a bit of code here for how you're using it exactly?