Hello dear community,
I’m currently building a component that should detect whether there is a surface under the player or not. The plan is currently to only use collider and raycasting.
I have now gotten the code to run so far that colliders that are located under the component are found.

However, when I use the code with a rotation, it seems to break. I don’t understand why this happens.

Does anyone here have an idea?
private bool SearchForSurface()
{
// Clearing the array so no old hits are used while iterating
System.Array.Clear(_raycastCache, 0, _raycastCache.Length);
if (_interactionObject.Collider is BoxCollider boxCollider)
{
var bounds = boxCollider.bounds;
int hitCount = Physics.BoxCastNonAlloc(bounds.center,
bounds.extents,
Vector3.down,
_raycastCache,
transform.rotation,
100f,
hitMask,
QueryTriggerInteraction.Ignore);
if (hitCount == 0)
{
Debug.Log("No hits");
return false;
}
for (int i = 0; i < hitCount; i++)
{
var hit = _raycastCache[i];
if (hit.collider == boxCollider)
{
continue;
}
Debug.Log($"HIT: {hit.collider.name}");
}
return true;
}
return false;
}
Please note that I’m only changing the rotation of the object during testing. No other values are changed.
Best regards,
Ben