# How to control a spaceship near a planet in Unity3D?

## How to control a spaceship near a planet in Unity3D?

```Right now I have spaceship orbiting a small planet. I'm trying to make an effective control system for that spaceship, but it always end up spinning out of control. After spinning the ship to change direction, the thrusters thrust the wrong way.
Normal airplane controls don't work, since the ship is able to leave the atmosphere and go to other planets, in the journey going "upside-down". Could someone please enlighten me on how to get thrusters to work the way they are supposed to?
```

I think your use of RigidBody physics for controlling the ship is an interesting idea (and might even be a very “correct” way of approaching this), however it’s probably much easier to implement the logic yourself. In your last comment, you asked about the basic concepts:

## Inertia / Momentum

Space ships have inertia, and since space is (for our purposes) frictionless, they will simply continue to travel in a straight line with their current speed. This direction and speed is their inertia, and can be represented by a 3D vector (a Vector3 in Unity). This vector is typically referred to as the object’s “velocity”. In Unity terms, you will want to update your ship’s position by adding this vector to it during every Update() call (remember to scale it by multiplying the vector by the deltaTime to cater for frame rate changes). Basically, our object will remember its velocity, and rotation and thrust will be used to change the velocity.

## Thrust

Whenever thrust is applied, it is simply added on to the velocity vector. The direction of this thrust vector is the opposite direction to the exhaust of your engine. If we’re talking about a “main engine”, and assuming we’re looking forward, this would simply be the current rotation of your spaceship. This update of the velocity happens during Update() method, before you apply the velocity to the current position. You’d scale the thrust vector appropriately with deltaTime, engine strength, the thrust level input from the pilot, and anything else you’d wish to add. Remember that we’re already “applying” the velocity to the position of the ship, so by updating the velocity, the position of the ship will be affected.

## Rotation

If we assume that we simply wish to rotate the ship in response to some user input (I mean: the rotation thrust is not going to be applied to the velocity as a “side-effect”), then things are relatively simple. Just as with aircraft, we have 3 primary axes around which you can rotate (refresher on Wikipedia), the pitch, yaw and roll axes. Fortunately, these correspond to the X, Y, and Z axes in the local space of the ship, meaning that we can perform a transform.RotateAround(SHIPCENTRE, AXIS, ANGLE).

If I remember correctly, transform.RotateAround operates in world coordinates, meaning that your rotation axis will need to be rotated into the ship’s local space. It would be something like:

transform.RotateAround(transform.Position, transform.TransformDirection(Vector3.up), 20 * Time.deltaTime);

The above should give you some idea of how a standard inertia and thrust based control system works.