2D Spaceship Thrust Control in Unity Part Three

Objective: Write C# code to control the left and right side thruster animations.

In part two of this series, we added thruster animations to our spaceship, the main thruster on the rear of the spaceship, and two side thrusters. The main thruster on the rear of the spaceship is always active. The two side thrusters are active only when the ship is turning. To do this we will have to write a few lines of code to our “Player” C# script.

We create two new “private Gameobjects” “ _leftThruster”, and “_rightThruster”. We add the [Serializefield] property so Unity creates two slots in the “Inspector” window.

By dragging the “LeftThruster” and the “RightThruster” into the newly created slots we “pass a reference or object handle to our “Player” script. Now the “Player” can access the components of our two side thrusters.

“_horizontalInput = Input.GetAxis(“Horizontal”); returns a float value between -1 and 1, to represent left and right. Using “if-else” we can now control our thrusters using the above code.

Houston we have control…Until next time……



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store