planetary gravity: falling "off"

now, my player stays within the vicinity of the planet, and when it gets below or above the global y axis (below an altitude of 0), the gravity for the player shifts so that instead of gravity pushing down, it pushes you up.

but you can't move the player at all, so you're stuck shifting between the two gravities, and unable top get back on land....

if there's a better way to make a mario style planet physics, where it looks like the planet rotates, but you the player or gravity itself changes so that the planet is always directly under you no matter where you go on the planet... then couold you give me a hint on how to script that? thanks.

here's my current script;

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 10.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
var MoveDirection : Vector3;

function Update() {
    my=moveDirection.y;

    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);

    if (grounded) {
        // We are grounded, so recalculate movedirection directly from axes
        moveDirection *= speed;

    } else {
        moveDirection = transform.position.normalized * gravity * Time.deltaTime;

        moveDirection.y=my;     
    }

    if (Input.GetKeyDown ("space")) {
        moveDirection.y = jumpSpeed;
    }
    if(transform.position.y >= 0)
    {
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
    }
    if(transform.position.y <= 0)
    {
     moveDirection.y += gravity * Time.deltaTime;
    }

    // Move the controller
    var controller : CharacterController = GetComponent(CharacterController);
    var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

A much better way of doing it is to calculate the actual vector between the player and the planet and use that as the gravity vector.

If your planet is positioned at the origin, then all you have to do is normalize the player position:

moveDirection = transform.position.normalized * gravity * Time.deltaTime;

If it's not at the origin, you just have an additional subtraction:

moveDirection = (transform.position - planetCenter).normalized * gravity * Time.deltaTime;

Update - To help explain, here is a simple example:

1) Create a sphere at (0, 0, 0)

2) Scale the sphere to (10, 10, 10)

3) Create a cube at (0, 0, -15) and give it a RigidBody component

4) Disable the gravity on the cube.

5) Apply the following script to the cube

using UnityEngine;
public class SphericalGravity : MonoBehaviour 
{
    float gravity = -9.8f;

    void Update () 
    {
        rigidbody.velocity += transform.position.normalized * gravity * Time.deltaTime;
    }
}

When you run the game, you should see the cube fall toward the sphere and then land on its surface.

the correct way to do the equator 'shift' is by the code that says:

if(transform.position.y >= 0)
{ 
 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;
}
if(transform.position.y <= 0)
{
 moveDirection.y += gravity * Time.deltaTime;
}

in the FPFlyer script.

I am currently trying to achieve exactly the effect discussed here... I have a first person view character and he should be able to walk on a sphere without falling off, and the ground should always appear below him. I'm new to scripting though and haven't been able to implement GlitchEnzos script yet on my First Person Controller. Can anyone provide a step by step walkthrough on how to do this?