Pew! Pew!

Firing lasers with Unity’s new Input System

Christopher West
4 min readApr 3, 2021

Yesterday, we looked at how we could apply Unity’s new Input System to the movement of our Player. We’ve previously looked at instantiating and destroying game objects as we made our Player fire lasers and, similar to our movement code, we used the default Input Manager that comes with Unity to accomplish that. Now we want to look at applying the new Input system to our firing mechanism too.

In our previous article, we added the OnMove and OnFire event handlers to our Player class. In that article, we added code to the OnMove event handler to retrieve the input values we applied in our player movement code. With our OnFire event handler in place, we are going to look at connecting it to our space bar for firing our player’s lasers.

Code Changes

Let’s look at our OnFire event handler. What we started with, when we first generated our OnFire method, is a method that simply throws an exception to let us know we need to do some work.

With our movement code, we needed to update our player’s position constantly and in our original fire code, we check every frame to see if the fire button has been pressed during that frame. With the new Input System, we are waiting for an event to be raised to tell us that the fire button has been pressed. This allows our code to be lighter from a performance perspective. Let’s add our code for instantiating a laser object when we detect that the fire button has been pressed.

We also need to remove our previous fire code from our Update method resulting in.

Correct our Input Map

You might have noticed that after adding our code above and running our game, that our space bar is not generating our laser! Oh no! The reason that our fire code isn’t working isn’t immediately apparent. When we created our Input Action Map we took the default mappings for our Fire map and in that default map the keyboard & mouse mapping for Fire is the left mouse button, not the Space key. We need to update our Input Action Map to use the Space key for our Keyboard & Mouse binding for Fire.

  1. Open our Input Actions editor by double-clicking on the asset file in the Project window
  2. Click on our Player map
  3. Expand the Fire action
  4. Click on the Add button (it looks like a “+” on the “Fire” node)
  5. Select Add Binding
  6. Click the “Path” drop-down control
  7. Either Click the “Listen” button and press the Space key OR type Space in the search box
  8. Select Space[Keyboard] from the results
  9. Check the Keyboard&Mouse checkbox under “Use in control scheme” to make sure that this binding doesn’t conflict with the same function in other control maps.
  10. Click Save Asset (if you don’t have Auto-save checked)
  11. Close the Input Actions Editor

You’ll now find that the space key works again, and we can now fire lasers with our new Input System. You may also notice that we now have a new…feature! We are unintentionally firing two lasers for every key press! While this is a cool effect, it isn’t what we intended. Let’s take a look at how to fix it.

So what’s happening here? The button type of input triggers twice, and thus so does our code that fires a laser. The button triggers once on the Actuation of the key and once on release. How can we determine in our OnFire code which situation is occurring? Luckily this information is stored in the context object that we receive in our OnFire event handler. The key down event can be checked with context.started and the key up event can be checked with context.canceled. Since I want our laser to fire off as soon as the Space key is pressed, we’ll check the context.started property like below

Next Time!

There we have it! We have swapped out the code that makes our Player fire lasers so that it now uses the new Input System! Tomorrow we’ll look at limiting the number of times the player can fire in a given time period by implementing a cooldown system! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

Christopher West
Christopher West

Written by Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

No responses yet