Search Unity

Question HoloLens 2 Spatial Mapping Raycast doesn't collide with spatial mesh

Discussion in 'Scripting' started by thevituu, Sep 28, 2021.

  1. thevituu

    thevituu

    Joined:
    Nov 22, 2020
    Posts:
    2
    Hello everyone,

    I am trying to move a sphere around in the HoloLens and if it collides with the spatial mesh it should change it's color. My problem is that the SphereCast I'm doing to see if it collides with the mesh does not work.

    Here is my code for this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SphereCaster : MonoBehaviour
    6. {
    7.     public GameObject currentHitObject;
    8.  
    9.     public float sphereRadius;
    10.     public float maxDistance;
    11.     public LayerMask layerMask;
    12.     public Transform sphere;
    13.  
    14.     private float currentHitDistance;
    15.  
    16.     private Vector3 origin;
    17.     private Vector3 direction;
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         origin = transform.position;
    23.         direction = transform.forward;
    24.         RaycastHit hit;
    25.         if (Physics.SphereCast(origin, sphereRadius, direction, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
    26.         {
    27.             sphere.transform.gameObject.GetComponent<Renderer>().material.color = Color.red;
    28.             Debug.Log(hit.distance);
    29.             currentHitObject = hit.transform.gameObject;
    30.             currentHitDistance = hit.distance;
    31.         }
    32.         else
    33.         {
    34.             currentHitDistance = maxDistance;
    35.             currentHitObject = null;
    36.             sphere.transform.gameObject.GetComponent<Renderer>().material.color = Color.green;
    37.         }
    38.     }
    39.  
    40.     private void OnDrawGizmosSelected()
    41.     {
    42.         Gizmos.color = Color.red;
    43.         Debug.DrawLine(origin, origin + direction * currentHitDistance);
    44.         Gizmos.DrawWireSphere(origin + direction * currentHitDistance, sphereRadius);
    45.     }
    46. }
    47.  
    It works totally fine for GameObjects but I cannot find a way to detect a collision with the spatial mesh. The Sphere I am moving around doesnt have to be stopped or something, it should just change its color.

    I hope you can help me out here.
    Thanks in advance!

    EDIT: The layer I am choosing is the Spatial Awareness layer.
    I am using Unity 2019.4.25f1
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,473
    For those of us who don't know, what exactly is "the spatial mesh". Physics queries are designed (unsurprisingly) to detect physics colliders so if the "spatial mesh" is a collider then it'll work otherwise why would it?
     
  3. thevituu

    thevituu

    Joined:
    Nov 22, 2020
    Posts:
    2
    The spatial mesh ist basically a scan of the surface in the room. The HoloLens is capable of scanning your environment and create a mesh out of it.
    Now that you said it, it makes sense that it doesnt work. The problem ist that i cannot attach a physics collider to the mesh. Or at least I don't know how.


     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    What does that mesh object look like in Unity, in terms of like components etc? If for example it's an object with a MeshRenderer and MeshFilter, then you can add a MeshCollider + rigidbody with isKinematic checked, and it should work.