# Moving object along its own axis

Essentially I've currently got an object which is on a planet (think mario galaxy). If I move the object then it rotates around the centre point of the planet. The problem I have got is in actually moving it. When I move it it moves along the standard axis so it pulls away from the planet like in part 2 of the below image. I want it so it moves along its own axis like part 3 of the below image. Basically i want it so it moves around the planet rather than moving out of it. Does anyone have any idea how to do this?

Heres my current code:

``````function Update () {
var xval : float = -iPhoneInput.acceleration.y;
var yval : float = iPhoneInput.acceleration.x + 0.1;

if(xval > 0.5){
xval = 0.5;
}

if(xval < -0.5){
xval = -0.5;
}

if(yval > 0.5){
yval = 0.5;
}

if(yval < -0.5){
yval = -0.5;
}

var offset : Vector3 = Vector3(xval / 8, yval / 8, 0);
rigidbody.MovePosition(rigidbody.position + offset);
}

``````

Do this:

``````function Update(){
transform.position += transform.TransformDirection(Vector3.right)*moveSpeed;

transform.LookAt(spherePosition);
}

``````

This makes your character move along its x-axis while always pointing at the middle of the sphere.

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

http://unity3d.com/support/documentation/ScriptReference/Transform.TransformDirection.html

One way would be to use the classic formula for a position on the point on a circle with a given radius. Right now you are simply calculating the position along one of the axis - you will need to calculate and modify several axis at the same time.

If the center of the planet is at 0,0,0 you can use pythagoras to calculate x,y,z.

I'm unsure if you can do this with manipulating the gravity settings instead, so that your rigidbody calculations do it all for you automagically.