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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereCaster : MonoBehaviour
{
public GameObject currentHitObject;
public float sphereRadius;
public float maxDistance;
public LayerMask layerMask;
public Transform sphere;
private float currentHitDistance;
private Vector3 origin;
private Vector3 direction;
// Update is called once per frame
void Update()
{
origin = transform.position;
direction = transform.forward;
RaycastHit hit;
if (Physics.SphereCast(origin, sphereRadius, direction, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
{
sphere.transform.gameObject.GetComponent<Renderer>().material.color = Color.red;
Debug.Log(hit.distance);
currentHitObject = hit.transform.gameObject;
currentHitDistance = hit.distance;
}
else
{
currentHitDistance = maxDistance;
currentHitObject = null;
sphere.transform.gameObject.GetComponent<Renderer>().material.color = Color.green;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Debug.DrawLine(origin, origin + direction * currentHitDistance);
Gizmos.DrawWireSphere(origin + direction * currentHitDistance, sphereRadius);
}
}
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
