- Home /
RayCast ignoring mesh colliders?
Hi,
I'd just like to reiterate this question from six months ago, as there was no answer. How do Ray- and Sphere- and WhatnotCast() treat mesh colliders?
My tests seem to suggest that the casts simply ignore mesh colliders. Mine was convex.
Thanks!
As mentioned below, I made a simple test scene consisting of a central object and a few orbiting colliders. The central object has a script that calls SphereCastAll() and the other objects have various kinds of colliders. It happens that of the mesh colliders, only the sphere and capsule meshes registered as hits. Can anyone confirm these results?
Just found out that Physics.SphereCastAll() ignores objects "behind" the starting point even though they're in the sphere. Nice.
It seems like that might've been my problem all along. The behavior of SphereCast() is VERY erratic when it comes to the placement of the origin.
Answer by microp · May 17, 2011 at 08:44 PM
SphereCasts have trouble detecting objects that are within the radius around their origin, especially those behind the casting direction. In the case of my 2d game, I started the SphereCast X units above the gameplay plane and made it go X units below. That seems to catch everything properly.
I definitely consider this a bug and lost a lot of time to it. How do I report it so that others don't have to go through all of this?
Answer by Eric5h5 · May 12, 2011 at 09:54 PM
Raycasts do not ignore any colliders, mesh or otherwise, unless set up to do so with layers. They will "go through" the back sides of polygons, just like the back sides of polygons are (usually) invisible.
That's very odd. The faulty object is on a layer that my SphereCast is set up to collide with. It's ignored when it has its mesh collider, but if I switch that for a sphere collider, and nothing else, everything's fine. Both are non-triggers, and normal collisions with rigidbodies work fine too. Only the SphereCast fails. I'll investigate further.
I don't believe it. I set up an empty test scene. It turns out that sphere and capsule mesh colliders are caught correctly, but any other mesh fails, built-in or custom.
I also have the issue with the type of collider that Raycast detect.. It detects on Box Collider & Sphere collider correctly , but not detecting $$anonymous$$esh or Capsule collider. It detects the capsule collider once in thousand clicks..
Here is my code:
void Update ()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftAlt))
{
RaycastHit hit;
Ray ray = new Ray (transform.position, transform.forward);
if(Physics.Raycast(ray,out hit , 1000f))
{
Debug.Log(hit.collider.gameObject.tag.ToString());
if(hit.collider.gameObject.tag == "Enemy")
{
Debug.Log(".. hit the enemy");
}
}
}
}