Hours of googling to no avail.
I have a level imported in and I’ve checked “Generate Colliders”. This means I’m using a Mesh Collider (NON-Convex) and I can’t use any other type of collider. And when I use my raycast it doesn’t detect the level as existing.
EDIT:
After narrowing further I’ve discovered that it’s ProBuilder. My level mesh was generated using ProBuilder and I’ve checked “Generate Colliders” but still the ray passes through it. After testing normal mesh collider’s the ray does indeed hit them
public Transform point;
Ray ray = new Ray(point.position, point.forward);
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject(ray, out hitPoint);
if (hitTransform != null)
{ // If we hit something
Debug.Log("Hit: " + hitTransform.name);
}
float maxRange = 10f;
Transform FindClosestHitObject (Ray ray, out Vector3 hitPoint)
{
RaycastHit[] hits = Physics.RaycastAll(ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits)
{
// If this thing we hit is not us and we haven't hit anything else or this is closer than the last thing we calculated for
// and if this is within range to even hit whatsoever
if (Vector3.Distance(hit.transform.position, transform.position) <= maxRange &&
hit.transform != this.transform &&
(closestHit == null || hit.distance < distance))
{
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}
Any and all help is appreciated as the bullets being able to hit the walls of the map is pretty important.