Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SphereCast -- what is RaycastHit.normal?

Discussion in 'Scripting' started by sgoodrow, Jun 14, 2014.

  1. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    I am seeing very strange results here. Casting against a flat surface at an angle can yield different normals depending on the cast start position. Furthermore, the normals themselves make no sense. I am getting the impression that the normal is some sort of contact point normal, rather than mesh normal. Is this the case?
     
    Last edited: Jun 14, 2014
  2. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,181
    According to @JasonL663 in this thread (which I am inclined to agree with), the
    If you use Debug.DrawRay(hitInfo.point, hitInfo.normal), you can get a clearer view of what is going on. Unfortunately, the normal appears to be a bit off. For instance, I have a sphere which does not rotate, and collides with a flat floor. This sphere should always collide on it's bottom center point, meaning the normal should always point perpendicular to the floor. As you mentioned, however, the normal changes depending upon the the angle of the collision. Perhaps this is intentional, but it just makes things harder for my own code, where I need to detect if a flat surface or edge was hit.

    Fortunately, the normals can be calculated manually to produce more useful data (at least in my opinion). To do so, start by calculating the position of the casted sphere (i.e., it's center) at the collision point.

    Code (CSharp):
    1. Vector3 collisionCenter = castOrigin + (direction * hitInfo.distance);
    Now you can calculate the normals, like so:

    Code (CSharp):
    1. Vector3 normals = (collisionCenter - hitInfo.point) / sphereRadius;
    Note: I am not a Physics expert, so there's a chance I'm missing something and this code is off. But it's working for me, so enjoy!
     
  3. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    Good thinking to reconstruct the normal from the sphere model. I agree that this should produce accurate results so long as the dependent data means what we think it means (it seems to, but I remain skeptical since the hitInfo.normal does not).
     
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,181
    Just something to keep in mind. The manually calculated normal does not always turn out to be perpendicular to the surface hit when the sphere strikes a flat surface (i.e., if the sphere hits a flat floor, the normal will not always be x = 0, y = 1, z = 0). Sometimes the value will deviate from 0 or 1 by about .0000001.

    So if you're trying to detect whether a flat surface was struct, make sure to take this deviation into account.

    The good news is this deviation is much less than the inherent introduced by the normal RaycastHit.normal value.