- Home /
How can i detect the normal of the collider the Character Controller is touching.
This documentation here: http://unity3d.com/support/documentation/ScriptReference/ControllerColliderHit-normal.html claims we can get "The normal of the surface we collided with in world space".
That is NOT the case! The normal we get seems to be the inverted normal of the point we hit on the curvature of the character collider capsule, rather than the surface normal of the object we collided with.
Example we walked (left to right) over the peak/edge of a large rotated cube. the Debug.DrawRay function drew those red lines for us. Once the character controller approached the peak the "surface normal" starts bending upwards and than bends downwards again on the other side. That is not what the surface normals of a cube look like. the result it should give us i photoshoped into the right screenshot in green.
If you need further proof /visualization on how the "output"normals change based on which part of the round character collider bottom hits it than have a look at screenshot2.jpg that shows us slowly walking off an edge. Also note how much more narrow the normal angel falloff is compared to the first screenshot further suggesting that this has nothing to do with the surface normals of the standard unity cube primitive
We could see practical application for both kinds of normal output so we are not arguing for removing the current one (that might break many other unity games that depend on it) but we desperately need the "real" functionality that works like the documentation claims it does.
Do you know how I can get the real normal of what I am colliding with without resorting to raycasting every frame? I already have a solution that uses a raycast (basically inverts the normal that I am provided and raycasts in that direction and then gets the correct one).
Answer by jonas-echterhoff · Oct 10, 2011 at 01:42 PM
Unfortunately, NVidia PhysX does not support getting separate surface normals for both objects in box/capsule collisions. You should get results more close to what you expect when you replace your BoxColliders with MeshCollider. This is not likely to change any time soon, as it is a limitation of PhysX, and probably not a very high priority issue for NVidia to change.
Answer by DaJuice · Dec 03, 2013 at 06:29 PM
There is a very simple way to achieve this:
Use a Raycast.
RaycastHit hit;
if (Physics.Raycast(ray, out hit, rayLength)) {
Vector3 normal = hit.normal;
}
Cast your ray downward from the center of your collider, the normal in a RayCastHit always represents the normal of the surface beign hit.
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-normal.html
EDIT: This will give you the green normals that you want, but it alone won't help you with the issue of your character slowly falling down an edge, although you could tweak your script based on raycasts so it behaves correctly.