Space Flight Simulator: Rigidbody Movement

Ryan McCoach
4 min readApr 1, 2023

--

Intro

In this article we are going to discuss how to use the Rigidbody for our space ship to movement around the 3 axes (pitch, roll & yaw) and thrust.

This script is a simplified version of a script created by Midnite Oil Software LLC. Please check out his channel and video

https://www.youtube.com/watch?v=VW3PkEF1Fzk&t=10s

Rigidbody

Adding a Rigidboy component to a game object will put the motion of that game object under the control of Unity’s physics engine. This is perfect for controlling the movement of our space ship.

Initially, we will add some drag which is the reactive or resistive force of thrust or an object moving. Along with drag, we will give some angular drag which is just drag caused by rotation. Lastly, we are going to turn off Use Gravity since we are in space.

Now that the rigidbody component is added, we want to be able to manipulate in our Ship Controller script.

We will store the Rigidbody component in a variable to access it

and cache it on the Awake method. The Awake method runs before the Start method. We want to grab the rigidbody first because the rest of the movement is based on the rigidbody, so we want to initialize it first before we start to control it.

Flight Forces

We are going to create a float variable for each of the four movement aspects of flight:

  1. Thrust — forward force
  2. Pitch — rotation on the x-axis
  3. Roll — rotation on the z-axis
  4. Yaw — rotation on the y-axis

We serialize these variables and give them a range.

Serizalizing allows access to private variable in the editor and the range allows the use of a slider to adjust the value of that variable between the min and max of the range.

These values determine the amount of force applied to each aspect of movement.

Force Amounts

We are going to something similar for each aspect of movement, but these variables determine the amount of the each movment to be applied to the space ship through the controls of the player.

The range for these amounts only range from -1 to 1 and will determine the direction of that movement. When the value of the variable = 0 that will mean no movement will happen, but if the value of variable increments to positive 1 it will move in one direction and if is decrements towards -1 it moves in the opposite direction.

Move It

When calculating physics, it is best practice to use the Fixed Update method, instead of the Update method, because “… physics updates are carried out in measured time steps that don’t coincide with the frame update. FixedUpdate is called immediately before each physics update and so any changes made there will be processed directly” (Unity Documentation).

We are only going to apply force if that force amount is NOT close to 0. The use of Math Function Approximately helps solve the problem of floating point imprecision, which makes comparing floats using the equals operator inaccurate. The approximately function compares two floats and returns true if they are within a small value of each other.

When these comparison returns not true, we use the rigidbody method of adding torque (rotation) to the axis of movement.

  • transform.right = x-axis
  • transform.up = y-axis
  • transform.forward = z-axis

We multiply the force of that axis by the force amount by Time.fixedDeltaTime (the interval in seconds at which physics and other fixed frame rate updates).

For thrust, instead of using the the AddTorque method we use the AddForce method (Force is applied continuously along the direction of the force vector).

--

--