# Movement around sphere like Mario Galaxies

I am making a prototype inspired by Mario Galaxies regarding that you walk around on ‘spheres’. I’m getting a lot of hazzle regarding the rotation for the player around this sphere. I want it to be rotated so that that player ‘down’ is always towards the ground or the center of the sphere, while also rotating around the players local y-axis for steering.

This is the code that controls rotation and movement

``````gravDir = asteroid.position - transform.position;
gravDir.Normalize();

float rotate = Input.GetAxis("Rotate");
float walk = Input.GetAxis("Walk");

float angle = Vector3.Angle(transform.up, -gravDir);
transform.RotateAround(transform.position, Vector3.up, rotate);
transform.RotateAround(transform.position, Vector3.right, angle);

rigidbody.AddForce(transform.forward * walk * 1000f * Time.deltaTime);
``````

As you can see I try to rotate the player around Y and X axis induvidual and this works fine as long as I do it one at a time, but if I try to walk and turn at the same time the rotation flips out on me.

I’ve tried using eulerangles to set the rotation, but I just can’t figure it out right now, so if anyone have any snippet or idea how to solve this I’d be very grateful

You will find some informations here to achieve some effects.

This is something added in the update method that did the trick for me.

**Sphere gravity is, you can also add this in the update method before ‘aligning’ your character to the normal of the surface:

//Spherical gravity (Attracted to a point)
// Apply Sphere gravity
//ROTATION ALIGNMENT

``````	sphereGravity = this.transform.position - Vector3.zero;
playerVelocity -= sphereGravity * 0.1f * Time.deltaTime;

Vector3 localUp;

localUp = this.transform.up;

Quaternion q = Quaternion.FromToRotation (localUp, sphereGravity);
q *= this.transform.rotation;

this.transform.rotation = Quaternion.Slerp (this.transform.rotation, q, 1.1f);
``````