perpendicular line to collision surface

I am trying to get a perpendicular line to the surface of an object the character collides with, however it looks like the collision normal is relative to the origin (always within 1 unit of the origin) so I cannot draw a line from collision point to the normal and expect a perpendicular line to the collision surface. Is that a normal behavior (no pun intended)?

I was able to get a perpendicular line to the surface of the object by adding the position of the object to the normal to get a “corrected” normal value and then draw the line from object’s origin to the new normal vector. I have a feeling that this isn’t a correct approach. Thoughts?

Blue lines are generated with the code below while the red line is a raycast from the collision point directly to the original normal.

Blue Lines

    void OnControllerColliderHit(ControllerColliderHit hit)
   {
       var point = hit.point;
       var dir = -hit.normal;
       // step back a bit
       point -= dir;
       RaycastHit hitInfo;
       // cast a ray twice as far as your step back. This seems to work in all
       // situations, at least when speeds are not ridiculously big
       if (hit.collider.Raycast(new Ray(point, dir), out hitInfo, 2))
       {
            var normal = hitInfo.normal;

            Debug.DrawLine(hit.transform.position, hit.transform.position + hitInfo.normal, Color.blue, 100f);
        }
   }

Red Line

Debug.DrawLine(forwardHitInfoTop.point, forwardHitInfoTop.normal, Color.red);

The collision normal is a normalized direction vector. yes it is expected that the magnitude of that vector is always 1, as it is intended to convey a direction only.

Sure you can:Debug.DrawLine(hitInfo.point, hitInfo.point + hitInfo.normal, Color.green);

4 Likes

that worked, thanks!