0

I have been stuck on this for hours. My movement code uses raycasting since it is a grid based. I started working with probuilder and it seems that the collider detection is quite unreliable and the mesh collider is not detected most of the time.

Here is the mesh collider and the raycast code:

bool CanMove(Vector3 direction)
{
    if (direction.z != 0)
    {
        if (Physics.Raycast(zAxisOriginA, direction, out hit, rayLength))
        {Debug.Log(hit.collider);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider);
                return false;
            }
        }             
        if (Physics.Raycast(zAxisOriginB, direction, out hit, rayLength)) 
        {
            Debug.Log(hit.collider);
            if (hit.collider != null)
            {
               
                return false;
            }
        }    
    }
    else if (direction.x != 0)
    {
        if (Physics.Raycast(xAxisOriginA, direction, out hit, rayLength))
        {Debug.Log(hit.collider);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider);
                return false;
            }
        }    
        if (Physics.Raycast(xAxisOriginB, direction, out hit, rayLength))
        {Debug.Log(hit.collider);
            if (hit.collider != null)
            {
                Debug.Log(hit.collider);
                return false;
            }
        }    
    }
    return true;
}
Nush
  • 55
  • 1
  • 7
  • What's the value of rayLength? Also, to clarify, are all Raycast calls returning false or is the `hit` data missing something (ie. collider = null)? – avariant Apr 07 '23 at 15:22
  • rayLength is a float of 1 since i need to find objects one grid square ahead of me. All debug calls from the RaycastHit hit are returning nothing. – Nush Apr 07 '23 at 15:48
  • If you debug into the code, are any of the hit fields populated? ie. `transform` or `rigidbody`? – avariant Apr 07 '23 at 15:58
  • No that's the problem it's giving not printing any debug logs. – Nush Apr 07 '23 at 16:04
  • Have you put in a breakpoint and actually checked what the resulting hit contains? And if you are simply getting *no* debug print outs at all, then that means the Raycast is returning false and never hitting anything all. If that is the case, try increasing your rayLength just to see if you actually hit something. – avariant Apr 07 '23 at 16:06
  • I have been doing just that and it does not seem to change anything. I think there is a problem with the collider because in some other probuilder made levels the controller seems to work perfectly fine yet in this one it does not? – Nush Apr 07 '23 at 16:10

0 Answers0