Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity wants to learn about your experiences working on a Unity project today. We'd like to hear from you via this survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  4. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Help Wanted Raycast in Unity MARS

Discussion in 'Unity MARS' started by Goan123, Jun 8, 2020.

  1. Goan123

    Goan123

    Joined:
    Apr 18, 2020
    Posts:
    1
    I would like to create a placement indicator. To do this I have to make a raycast from the center of the camera in MARS simulation. How can I do this?
     
  2. leweyg_unity

    leweyg_unity

    Unity Technologies

    Joined:
    Jan 29, 2020
    Posts:
    38
    Hey @Goan123, so the easiest way to use the “MARS Plane Visualizer” and simply customize the “Plane Prefab” to support collision detection. Details below:

    • First make your plane prefab (empty game object, then GameObject Menu / 3D Object / Plane to add a plane under it, use a material that repeats in the UV texture space).
    • Now create a “MARS Plane Visualizer” (just add that component or choose: GameObject Menu / MARS / Data Visualizers / Plane Visualizer”, and set the “Plane Prefab” to the prefab you created above.
    • At this point you should get planes that you can Physics.RayCast against to get collisions, and it should appear in the Simulation View as well.

    If you want your custom cursor code to also work in the Simulation or Device views, simply add the “Unity.MARS.ISimulatable” interface to your cursor behavior, and it will run in the Simulation Scene, as well as in Play mode.
     
    wickedwaring likes this.
  3. leweyg_unity

    leweyg_unity

    Unity Technologies

    Joined:
    Jan 29, 2020
    Posts:
    38
    By the way, to put a proxy into the center of the view you can also use Forces, get a proxy that spawns onto horizontal plane, then select "Forces / Add Force..." in the Inspector and add "Align to Camera", as well as "Snap to Horizontal/Vertical Surface". This is for play mode, but will influence the object to be centered in front of your user, and on a surface (it's not as 100% as ray casts, but easier to setup).
     
    JohnnyOkami and jmunozarUTech like this.
  4. jmunozarUTech

    jmunozarUTech

    Unity Technologies

    Joined:
    Jun 1, 2020
    Posts:
    207
  5. unity_andrewm

    unity_andrewm

    Unity Technologies

    Joined:
    Jun 25, 2015
    Posts:
    69
  6. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    959
    Is there a way (existing template?) to have MARS adapt to what ARKit would behave/do for plane finding / mesh classification?
     
  7. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    959
    The plane visualizer appears visually but raycast hit transform seems to still return null...
     
  8. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    959
    Was hoping Unity MARS could fully simulate ARKit environments for in-editor testing. However, after fiddling quite a bit and watching all the tutorials and reading the docs, I cannot seem to get raycast hit to return (also, there just aren't enough people using it in the community yet - something as basic as this shouldn't be so obscure).

    So I am just going back to the indie solution - ARFoundation Remote https://assetstore.unity.com/packages/tools/utilities/ar-foundation-editor-remote-168773
     
  9. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    47
    Re raycasting: If you are using regular Unity physics raycasting then if you add a mesh collider to the prefab that the plane visualizer uses then you should get hits.

    If you are using the MARS IUsesMRHitTesting interface and not getting hits that's a known issue that we are working on.
     
  10. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    47
    And on the subject of AR Foundation simulation, as of MARS 1.2 (released last week) we do support simulation for AR Foundation in the editor. This is a very new feature and not everything is supported yet, but we'd love to have you try it out and give us feedback.
    If you get MARS 1.2 and enable it in Project Settings > XR Plug-in Management then when enter play mode you should be able to test AR Foundation scenes against MARS's simulation environments.
     
  11. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    959
    Thanks for the reply. So I tried that - *visibly* ticking that MARS Simulation checkbox in XR Plugin-Management... but I am still not getting a raycast return? I am using ARFoundation's ARRaycastManager.

    Raycast returns on actual device and using the indie creator's ARFoundationRemote from the Asset Store
     
  12. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    959
    I am just using ARRaycastManager - which returns on both device and on ARFoundationRemote... I have yet to be able to get raycast to work on MARS https://docs.unity3d.com/Packages/c...yEngine.XR.ARFoundation.ARRaycastManager.html

    Also, I notice that in Simulated Environment scene, the Kitchen_13ftx9ft includes colliders...

    Does MARS only work with Physics.Raycast?

    Why isn't Unity's own ARFoundation supported?
     
  13. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    47
    That's not good - it should work :(

    When you enter play mode has MARS discovered the planes - if you add a plane visualizer is it showing planes where you are raycasting?

    arf-raycast.gif
     
  14. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    959
    Can you let me know if there are only certain versions that this works on? What is the tested / recommended MARS, AR Foundation, Unity version?
     
  15. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    47
    Should work with any versions that MARS supports. I just tested Unity 2019.4.16f1 with AR Foundation 2.1.10 and MARS 1.2.

    Do you have a scene or project you can share?
     
  16. chanfui

    chanfui

    Joined:
    May 2, 2017
    Posts:
    10
    Hi I can't find the files. Using latest MARS plugin (Just started using it 2 weeks ago).
     
  17. jmunozarUTech

    jmunozarUTech

    Unity Technologies

    Joined:
    Jun 1, 2020
    Posts:
    207
  18. wiltabone

    wiltabone

    Joined:
    Oct 30, 2015
    Posts:
    9
    Hi, In relation to this post (https://forum.unity.com/threads/depth-calculation.1073873/), I have been trying to get the distance from a ray shot from the centre of the screen (ie. device) to a surface in the real world.
    I have tried replacing the prefab of the planes visualiser but I am still getting null results in play mode.

    I am using the following script (attached to an empty game object in the MARS scene).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.XR.ARSubsystems;
    6.  
    7. public class RaycastLiDARHandler : MonoBehaviour
    8. {
    9.     private ARRaycastManager arRaycastManager;
    10.  
    11.     private static List<ARRaycastHit> hits = new List<ARRaycastHit>();
    12.  
    13.     private Vector2 rayPos = new Vector2(Screen.width/2, Screen.height/2);
    14.  
    15.     private float distance;
    16.  
    17.    
    18.     void Start()
    19.     {
    20.        
    21.     }
    22.  
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         //if (arRaycastManager.Raycast(rayPos, hits, TrackableType.Planes))
    28.         if (arRaycastManager.Raycast(rayPos, hits))
    29.         {
    30.             distance = hits[0].distance;
    31.             print("DISTANCE: "+distance);
    32.         }
    33.     }
    34. }
    35.  
     
  19. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    47
    I think you're getting null reference errors because arRaycastManager is never initialized.

    If you add an AR Raycast Manager component to the MARS Session (it wants to be at the session origin) and then reference it in your script (I just made arRaycastManager public and dragged the session to it in the editor) it should work.
     
    wiltabone likes this.
unityunity