Unity Coder Corner

UCC is the one stop shop for everything Unity3d. From tutorials, to news, to opinon pieces. Find…

Follow publication

Coding Flight Controls in C#

--

Objective: An overview on coding the pitch, yaw, roll, acceleration, and deacceleration of a spaceship in the Unity 3D Engine.

Table of Contents

· How does flight work?
· Coding the Pitch, Yaw, and Roll
The Pitch
The Yaw
The Roll
· Acceleration
· A Camera that Brings it All Together

How does flight work?

Flying ships or aircraft can move in three axes, each responsible for a different behavior. Ships can perform 360° rolls, pitch upward and downward to change altitude, and yaw left or right to alter course.

Image Source

Coding the Pitch, Yaw, and Roll

In Unity, we can use our gizmo to determine in what position each movement type takes place.

Image Source

All the behaviors we need to code are rotations around a specific axis.

The Pitch

The upward and downward movement takes place on the X-axis, I know we’re used to horizontal changes happening here, but by comparing the plane’s movement and the gizmo, it’s plain to see that the changes occur in X.

In Unity’s Input Manager, I created a new setting called Pitch, and assigned the WS and up/down arrow keys.

Now all we have to do is access the input via code. On line #4, we have a lowercase pitch variable that stores the calculation needed to register the correct input and behavior. A global variable is used to control the rotation speed and multiplied by Time.deltaTime for a smooth application. For the input, I’m using the Pitch Input setting I created, this will register the assigned keys.

On line #11, we use the transform’s(the object’s) rotate to calculate the changes in rotation. transform.Rotate is a vector 3, meaning it takes 3 axes(x,y,z), as such, we place pitch on X.

The Yaw

For the Yaw, I used the preset Horizontal input setting in the Input Manager and assigned AD and the left/right arrows as input.

The code follows the same logic for the Pitch, however, this time we work the Y axis. This is observable in lines #6 and 11.

The Roll

I created one last input setting in the Input Manager called “Roll” and assigned the QE keys. This is identical to the pitch otherwise.

Now we work the Z-axis in-code and complete our set.

Acceleration

So, we got things rotating, how do we move forward? Let’s establish that our baseline speed is 0, we can store that value in a global or local variable. Now, all we need to do is register user input that changes that changes that value.

If our speed is less than one (having our baseline in 0) the speed increases to 40. Now we can use this value to move our object forward.

To stop or slow down, we check that the speed is greater than 0 then change the value accordingly, and register some input for it.

Note: GetKeyDown is usually prefered for input, however I am using get KeyUp because it is a single button press to engage in diffrent speed levels. I want the player to menauver all the diffrent rotations avaliable without having to worry about holding a button down to move forwards.

A Camera that Brings it All Together

The flight controls would be nothing without the epic camera shots. By using Cinemachine, I’ve created an amazing camera that follows the ship’s movements and even rotates along with it creating some amazing shots of the world.

The camera is a 3rd person cinemachine virtual camera using the Player as Follow and Look At Target, as well as using a Lock To Target Binding Mode to ensure it mimics the rotations of the ship.

Thanks for checking out my Unity Documentation! I’m a game developer looking for opportunities in the video game industry. Check out my portfolio(work in progress!) at dennissepagan.com

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Unity Coder Corner
Unity Coder Corner

Published in Unity Coder Corner

UCC is the one stop shop for everything Unity3d. From tutorials, to news, to opinon pieces. Find everything related to Unity3d here.

Dennisse Pagán Dávila
Dennisse Pagán Dávila

Written by Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. Portfolio: dennissepagan.com

No responses yet

Write a response