Need help with raycast detecting object in ar

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(); //for debugging
}

[SerializeField]
ARRaycastManager m_RaycastManager;

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

///

///

//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!

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

Here is some sample code that I’ve used to select objects in AR.

private Vector2 touchPosition = default;
Camera m_MainCamera;

private void Start() {
m_MainCamera = Camera.main;
}

void Update() {
if(Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
touchPosition = touch.position;

//Checking to see if the position of the touch is over a UI object in case of UI overlay on screen.

if (!touch.position.IsPointerOverUIObject()) {
if (touch.phase == TouchPhase.Began) {
Ray ray = m_MainCamera.ScreenPointToRay(touchPosition);
RaycastHit hitObject;

if (Physics.Raycast(ray, out hitObject)) {

//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.
if(hitObject.transform.CompareTag("case") {
//Do something with the case
}
}
}
}
}
}
1 Like

Thank you @AlexCWesterberg

Let me know if it doesn’t work as intended

1 Like