- Home /
Rotation around a planet not quite working
I am rotating a GameObject around a planet so it can be placed.
The horizontal works perfectly but I can't get the vertical to work properly.
I am not using a character controller and gravity is turned off on the rigidbody. I have locked the X, Y, Z for Position and Rotation so that the object is always facing the planet. I have an additional script that keeps the camera always above the object being placed.
Here is my Update code for the object:
MoveHoriz = Input.GetAxisRaw("Horizontal") ;
MoveVert = Input.GetAxisRaw("Vertical") ;
transform.RotateAround(Vector3.zero, new Vector3(0, -1, 0) * MoveHoriz, 50 * Time.deltaTime);
transform.RotateAround(Vector3.zero, new Vector3(-1, 0,0) * MoveVert, 50 * Time.deltaTime);
Answer by robertbu · Jul 30, 2013 at 06:31 PM
Solving these problems can be tricky and game dependent. The issue is that you are rotating around world axes. So if your object is on a world axis, then the object will spin in place and not move. You would have the issue with "Horizontal" as well if you moved first to the size of your sphere before you did any horizontal movement.
Assuming your character is standing on the sphere, with negative 'y' down, you can use forward and right as the axes:
void Update () {
float moveHoriz = Input.GetAxisRaw("Horizontal") ;
float moveVert = Input.GetAxisRaw("Vertical") ;
transform.RotateAround(Vector3.zero, transform.forward * moveHoriz, 50 * Time.deltaTime);
transform.RotateAround(Vector3.zero, -transform.right * moveVert, 50 *
Time.deltaTime);
}
Thanks robertbu,
When I used your code, Vertical was working but not horizontal. Then I realized that the axis for the object was off. I fixed that and it worked a lot better. However, horizontal made it just spin on the Y axis. I guess my axis is not quite right but then I changed the horizontal code to transform.up ins$$anonymous$$d of transform.forward and it works.
It gets a little wonky at the poles but it's close enough.
It shouldn't be wonky at the poles. Post your code back and I'll take a look. In order for the code to work correctly, you must have your model setup correctly. That is with rotation (0,0,0), the forward of the model must face positive 'Z' and the feet must face negative 'Y'. Try starting the model on the north pole. Note I ran a quick test of the above code before I posted it, and there was no wonkiness at the poles.
Your answer
Follow this Question
Related Questions
Planetoid Gravity 1 Answer
Third Person Camera on a speherical world 2 Answers
Constant rotation? 2 Answers
3d gravity on a planet 3 Answers
Orbit a camera around a small planet: there is space for improvement 1 Answer