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.
Prefabs
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!
Adding a New Input Action
A couple quick steps assuming you’re using Unity’s new Input System.
- 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”.
- Add a new Action. We’ll call it “Fire” and set its Action Type to “Button”.
- 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.
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.
- Add a
GameObject
variable with[SerializeField]
in your Player script:[SerializeField] private GameObject _laserPrefab;
- 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()
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.