Search Unity

[Solved] Raycast doesn't hit collider when rotated

Discussion in 'Physics' started by WhiskyJoe, Oct 23, 2019.

  1. WhiskyJoe


    Aug 21, 2012
    I'm having this weird issue at the moment where I am not sure if this is actually a bug, or if it's me doing something wrong/overseeing something.

    My situation is as following:

    I have a character that is moving around on a grid. For every change in a cell, I perform 2 raycasts to check if something is ahead of me and if I'm still grounded. The function I have for that is:

    Code (CSharp):
    1.     private bool HitTraversableSurface(Vector3 position, Vector3 direction, out Vector3 hitNormal, Color color)
    2.     {
    3.         hitNormal = Vector3.up;
    5.         Ray ray = new Ray(position, direction);
    7.         Debug.DrawRay(position, direction, color, 1);
    8.         RaycastHit hit;
    9.         if(Physics.Raycast(ray, out hit, .5f, 1 << LayerMask.NameToLayer("Traversable")))
    10.         {
    11.             hitNormal = hit.normal;
    12.             return true;
    13.         }
    15.         return false;
    16.     }
    This is working as expected and I had no issues, until I tried to move over a bridge in our level and the raycast would miss. The bridge has a collider with the right layer and connects properly with all the other colliders (that are not causing any issues) and the only difference is that the bridge (and so also the collider) is rotated 90 degrees on the y-axis to make the planks look right.
    When I rotate the bridge 90 degrees back, everything works!

    Now I can fix this with parenting, but I just want to know why this works the way it does. It doesn't really make any sense to me.

    For reference, this is an overview of the level:
  2. Maeslezo


    Jun 16, 2015
    I think it isn't a rotation problem

    You can check:
    The ray origin. Sometimes, the origin is already inside the collider, so the ray doesn't hit. Just for testing, try setting the ray origin much higher, increasing the ray length too
    Use the Physics Debug to be sure of the collider position:
    Last edited: Oct 25, 2019
  3. WhiskyJoe


    Aug 21, 2012
    I would find it very strange if the rotation is indeed the problem, but at the moment that's the only change I can make to make it work (or break). It's also not just the bridge, but in every situation where a collider is rotated (As long as the colliders' forward is similar to positive or negative world z, it's alright)

    I have been using
    to check the origin and direction of the ray and they are as expected and appear to be above the collider.

    The collider is also not scaled negatively, so I can also rule that out.

    I will do some more debugging with the Physics Debugger, I wasn't really aware of that and will help with visualizing, thanks for pointing it out!

    If that doesn't yield anything, I guess I will have to start reproducing this in isolation and see if I can reproduce the issue.
  4. WhiskyJoe


    Aug 21, 2012
    Well, seems like I found the problem:

    I am raycasting from a Y position of 0.5 to something that is on a Y position of 0. Considering the max value I raycast from is 0.5 it will be just barely be hitting the surface. I guess I got lucky on all the other surfaces, but the ones rotated probably had some floating point rounding going on.

    Either way, I changed the max range I raycast from to allow a bit more room and now it's working fine!

    I know it's some time ago, but still thought it would be a good thing to let anyone know who might stumble upon a similar issue.