Search Unity

Question Need help with raycast detecting object in ar

Discussion in 'AR' started by Deelvesh, Sep 7, 2021.

  1. Deelvesh

    Deelvesh

    Joined:
    May 21, 2016
    Posts:
    5
    Hi,
    this ar program spawns a model of a router consisting of a case , baseplate and a motherboard
    im trying to select the case of the router after placing the router on a plane in ar .
    the programs compiles no issues
    i can place router on a plane
    but then not matter what i can seem to use a ray to hit the Router case.

    please help !

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR.ARFoundation;
    using UnityEngine.XR.ARSubsystems; //to access ar raycast
    using UnityEngine.UI;
    using TMPro; //for debugging text box


    [RequireComponent(typeof(ARPlaneManager))]
    public class PlaceObjOnPlane : MonoBehaviour
    {
    //using a textbox for logs since cannot display logs on mobile
    public TextMeshProUGUI Text;

    // Start is called before the first frame update
    void Start()
    {
    Text = FindObjectOfType<TextMeshProUGUI>(); //for debugging
    }


    [SerializeField]
    ARRaycastManager m_RaycastManager;

    // Static list of raycast hits for AR planes
    static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();

    /// <summary>

    /// </summary>

    //Game object to put on scene
    [SerializeField]
    GameObject m_ObjectToPlace;

    [SerializeField]
    private Material highlightmaterial;
    //to place only one router
    private bool isRouterPlaced = false;

    //Raycast hit object
    RaycastHit hitinfo;

    //layermask to seperate planes and router object
    private LayerMask Router;

    // Update is called once per frame
    void Update()
    {

    //Text.text = " starting";//for debugging

    //make sure touch count > 0
    if (Input.touchCount > 0)
    {
    Touch touch = Input.GetTouch(index: 0);



    //hit for object physics & s_Hits for AR planes

    // check for only first touch ( else touches will keep on spawning objects)
    if (touch.phase == TouchPhase.Began)
    {
    //physics raycast to select parts of routers etc
    //ARSession camera tagged as main camera
    Ray rayToCast = Camera.main.ViewportPointToRay(touch.position); //shooting off touch point

    Text.text = touch.position.ToString(); //debug touch position of Raycast

    //Mathf.infinity cast ray to max distance
    if (Physics.Raycast(rayToCast,out hitinfo, Mathf.Infinity,Router))
    {
    Text.text = " ray out";//for debugging

    if (hitinfo.transform.gameObject.tag == "RouterCase")
    {
    Text.text = " ray hit";//for debugging

    }
    }
    else
    {
    Text.text = "no hit";//for debugging
    }
    //AR ray casting from touch position on screen to found plane
    if (isRouterPlaced == false) //check if router has already been placed
    {
    if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinBounds))
    {
    //store pose
    Pose hitPose = s_Hits[0].pose;
    //use pose for position and rotation of object spawned
    Instantiate(m_ObjectToPlace, hitPose.position, hitPose.rotation);
    isRouterPlaced = true;
    }

    }



    }
    }
    }
    }


    thanks for any help!
     
  2. Deelvesh

    Deelvesh

    Joined:
    May 21, 2016
    Posts:
    5
    Asked same question on another site and a really helpful person said to make this change

    Ray rayToCast = arCamera.ScreenPointToRay(touch.position);

    but it still doesnt work
     
  3. AlexCWesterberg

    AlexCWesterberg

    Joined:
    May 6, 2021
    Posts:
    7
    Here is some sample code that I've used to select objects in AR.


    Code (CSharp):
    1.  
    2. private Vector2 touchPosition = default;
    3. Camera m_MainCamera;
    4.  
    5. private void Start() {
    6. m_MainCamera = Camera.main;
    7. }
    8.  
    9. void Update() {
    10. if(Input.touchCount > 0) {
    11. Touch touch = Input.GetTouch(0);
    12. touchPosition = touch.position;
    13.  
    14. //Checking to see if the position of the touch is over a UI object in case of UI overlay on screen.
    15.  
    16. if (!touch.position.IsPointerOverUIObject()) {
    17. if (touch.phase == TouchPhase.Began) {
    18. Ray ray = m_MainCamera.ScreenPointToRay(touchPosition);
    19. RaycastHit hitObject;
    20.  
    21. if (Physics.Raycast(ray, out hitObject)) {
    22.  
    23. //Do whatever you want to do with the hitObject, which in this case would be your, well, case. Identify it either through name or tag, for instance below.
    24. if(hitObject.transform.CompareTag("case") {
    25. //Do something with the case
    26. }
    27. }
    28. }
    29. }
    30. }
    31. }
    32.  
     
    Deelvesh likes this.
  4. Deelvesh

    Deelvesh

    Joined:
    May 21, 2016
    Posts:
    5
  5. AlexCWesterberg

    AlexCWesterberg

    Joined:
    May 6, 2021
    Posts:
    7
    Let me know if it doesn't work as intended
     
    Deelvesh likes this.