Unity Dev: Playing with Fire

Let’s make our cube do something other than moving around. Let’s make it fire some lasers.

For quick prototyping, let’s Create > 3D Object > Capsule in your hierarchy. Call it “Laser”, resize it to maybe 1/4 the default size, then create a new material and give it a fun colour.


A great utility in Unity is the concept of “Prefabs” or “prefabricated” objects. You can create a single instance of a GameObject, complete with customized components and then use it over and over again, with the exact same configurations. Seems like something we’d want to do with our lasers considering we’re gonna be shooting a LOT of them.

Drag and drop the “Laser” GameObject into a new folder called “Prefabs”. You now have a prefab object you can use and load into your scene as many times as you like!

Pew pew pew!

Adding a New Input Action

A couple quick steps assuming you’re using Unity’s new Input System.

  1. Open your player Input Action’s object. If you’re following along from my Player Movement using the new Input System post, you might have named it “Controls”.
  2. Add a new Action. We’ll call it “Fire” and set its Action Type to “Button”.
  3. Click the “+” to Add Binding. You can add as many as you want. I’ve set up the Left Button (Mouse), Space (Keyboard), and Button South (Gamepads) that will trigger this Action.

Set up a blank void OnFire() function (seen below) and apply it to the Fire event in the Player Input component on the Player object.

Top-tier pseudo-code

Giving our Laser to our Player

We need to let our Player script know about our laser before we can expect it to do anything with it.

  1. Add a GameObject variable with [SerializeField] in your Player script: [SerializeField] private GameObject _laserPrefab;
  2. Unity Editor: Drag Laser from your Prefabs folder into the “Laser Prefab” configurable slot that’s now defined in the Player (Script) component on your Player game object.

Instantiate the Pew

We’re just a few steps away from making our wonderful Cube friend have the ability to create small pill shaped objects where it wants!

Let’s keep our code clean and reusable. Define a new void with a name descriptive of the fact that we’re instantiating a laser.

Here we’re passing our _laserPrefab variable to the Instantiate()method. transform.position as the second argument is letting it know to spawn on the Player’s current position (since we’re in the Player script), and Quaternion.Euler(0, 0, 90) is rotating the pill 90 degrees on the z-axis so that it’s facing in the correct direction to fire off to the right.

Next, we need to have our void OnFire() call our InstantiateLaser() function.

To do this, we pretty simply call InstantiateLaser() inside of our OnFire() function.

Obviously, you’re wondering what if (!context.performed) return; is all about. In any key press that gets fired, there are actually 3 events that will go off (context.started, context.performed, and context.cancelled). This can wind up with your code executing multiple times in very rapid succession.

To counter this, we just want to run InstantiateLaser() when context.performed happens. With that in mind, our first line of code checks to see if the event is NOT context.performed and returns (exits the function) if anything other than context.performed is happening. If it IS context.performed it will move on and run the code in InstantiateLaser()

Now with laser power!

We’ll get the lasers moving in another article! For now, enjoy the new power in the palm of your hand. Or foot pedal. Or whatever you’re using to trigger your input action.




On a quest to become a game developer. Still sometimes providing unsolicited advice about how to function in society.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Future of ECM: Content Services & Systems of Understanding

Fast & Beautiful Surfel Rendering on a Smartphone

Use layers to create your own LUTs

WTIA Virtual Career Fair on October 27

3 things your remote team can do now to provide better customer support

AWS Cloud Journey — Analyzing to Deploying Models with Amazon SageMaker (Ep. 1)

Mocking Systems Resources in C#/ASP.NET

eInfochips Roadmap in Lower Technology Nodes from 180nm to 7nm Beyond [Infographic]

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
Alex Somerville

Alex Somerville

On a quest to become a game developer. Still sometimes providing unsolicited advice about how to function in society.

More from Medium

Want a Professional Unity Layout?

Space Cadet VR — Adding Subtitles

Determining How Long Powerup Effects Should Last

Why prototype without assets?