# How can I make my player run around small planets?

I am attempting to make my player run around small planets, similar to Mario Galaxy, but only on spherical planets. I have set up a scene like so:

I have managed to get the player to move around the planet, by aligning it to the surface normals like in the code below

``````Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
Vector3 moveVector = inputVector * moveSpeed;

if (planet)
{
RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), -transform.up, 10f, planetLayer);

if (hit)
{
transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
}

moveVector.y -= planetInfo.pullStrength * Time.deltaTime;

transform.Translate(moveVector * Time.deltaTime);
}
``````

My problem is that I would like the player to be able to jump out of a planet’s orbit, and not be affected by gravity until it falls into the orbit of another planet.

I’ve tried some things involving rigid bodies, but it just hasn’t worked so well.

Ok, so generally speaking, for this to work you can use either a rigidbody or characterController, but they can’t use gravity.
Each planet needs a sphere collider that is larger than the planet itself, as a trigger, which represent the gravitational field. Continuously check if the player is inside the field, and if yes, apply force toward the planet depending on the distance (and possibly size of the planet), basically simulating gravity.

Now, when the player is on a planet aligned to the surface, and he jumps, apply force to him away from the planet’s core (or surface normal). The jump force needs to be higher than the gravitational pull, of course. Since we continuously check if the play is in a gravitational field trigger, as outlined above, once he leaves the planet’s field, the force towards the planet is no longer applied, so if there’s any force left from the jump impulse, that force will make the player continue flying away from the planet.

Add a script to each planet that checks for distance to the player. Then consider that if the player is within reach then grab a method on the player that will divert its path. That force vector could be:

`````` Vector3 direction = centerPlanet.position - player.position;
float force = 1f / Mathf.Sqr(Vector3.Distance(direction));
Vector3 gravity = direction.normalized * force;
``````

then when you get closer to the planet you can rotate the player with the same vector aligning the transform.up with the invert of the force vector (or the transform.down with your vector).

In order to leave the planet, you just need to make the jump force higher than the gravity force. Then start a long process of designing the appropriate gravity and jump forces.