Galaxy Shooter: Speed Boosting Thrusters
In this quick article I’ll show how I added the ability to press the left shift key for a boost from the thrusters in my 2D Space Shooter Unity game. (Any other key and/or button can also easily be used with the new input system)
I’m using the new input system, you can see how I have it setup in my previous article.
I first went to my Input_Manager in the hierarchy, then in my Player Input component I double clicked my Controls InputActionAsset to open it.
I then clicked the + to the right of Actions and created a new one named “Thrusters” and added a binding for the left shift key. Checked the box for Keyboard&Mouse control scheme.
Now in my Player class I get an error that the OnThrusters method has not been implemented, clicked the lightbulb icon at the error underlining Controls.IPlayerActions
in Visual Studio and implemented it, this adds the method to the bottom of the script.
I also added a private float variable for thrusters boosted speed multiplier.
Now instead of messing with my CalculateMovement()
method I simply modify my _speed
variable in my OnThrusters method by setting _speed equal to _initialSpeed
multiplied by my _thrustersBoostedSpeedMultiplier
when the Action is started(key has been pressed) and then reset the _speed
to the _initialSpeed
when the Action has been canceled (key has been released).
Last but not least don’t forget connect it all by dragging your Player into the events section in your Input_Manager’s Player Input component Events>Player section and select the OnThrusters method in your Player class. I spent some time wondering why nothing was working when I forgot to do this.
In a future article I’ll be reworking it to work with a thrusters meter bar in the UI that will limit the thrusters use to a certain amount of time before they need to be recharged.