Aligning object to surface normal not working upside down

I have an object (currently just a cube) that is constantly raycasting down, getting the normal of the surface below it, and adjusting it’s rotation to match it. In addition, I have a constant gravity force pushing the cube (which has a rigidbody attached) towards the surface. With a taller than normal cube collider it creates a nice “hovering” effect that, in theory, should work on any surface regardless of orientation.

However, it’s not. It works normally on “right side up” surfaces and hills, but it doesn’t seem to work when going upside down. I created a large sphere to test it out on - as you move the cube farther and farther towards the south pole of the sphere, it’s distance from the surface of the sphere mysteriously increases. The cube continues to rotate correctly to match the surface of the sphere, but I can see in the scene view that the collider is drifting farther and farther from the sphere. I’m not sure why gravity seems to lose it’s effect, and I don’t know if my issue lies somewhere with the rigidbody.AddForce or with the raycasting. Here’s part of my code:

var hit : RaycastHit;
var theRay : Vector3 = Vector3.down;
// If there's something beneath the vehicle...
if (Physics.Raycast (transform.position, theRay, hit)) 
	    // Calculate the angle under the vehicle controller
		var fro : Quaternion = Quaternion.FromToRotation (vc.up, hit.normal) * transform.rotation;
	    // Match the vehicle controller to the previously calculated angle
	    vc.rotation = Quaternion.Lerp (vc.rotation, fro, Time.deltaTime * rotationSmoothing);
	    // Push the vehicle towards the last encountered normal
        vc.rigidbody.AddForce (-hit.normal * gravForce);

It seems to me this should work, even upside down, but I’m obviously overlooking something. The cube’s rigidbody dosen’t use gravity, so that’s not it.

Anybody have any ideas?

Well, I figured it out on my own. For anyone wondering, Vector3.down is global; I needed local, or -transform.up. So the raycast was always facing the same direction, even as my cube rotated, meaning as it got to around the halfway point of the sphere it stopped hitting anything.

Also, it turns out if you make a sphere object big enough, the attached sphere collider will extend considerably farther than the actual polygons of the mesh. This made it look like my cube wasn’t on the surface, even though it was - it was just on the surface of the sphere collider. Using a mesh collider instead solved that problem.

Live and learn!