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!