Creating a 2D Beam Weapon in Unity
Creating a beam weapon can be a bit less straightforward than creating a laser projectile. With the laser, you can create the projectile and move it, and then just wait for it to hit something. The beam is more complicated to create because it has multiple states, and they shouldn’t all damage the player. Let’s take a look at a simple beam weapon setup.
Creating the Visuals
Let’s start by creating a new empty object and calling it Beam. Then we’ll create another empty, call it Beam Sprite, and parent it to the Beam. Let’s add a Sprite Renderer to our Beam Sprite object and drop in our laser sprite. Then we’ll scale it to the length of the screen from top to bottom.
Since our Beam Sprite object is separate from the base Beam object, we can easily scale it in an animation without causing any issues with the base object. However, this doesn’t mean we need to put the Animator component on the Beam Sprite object. This will be important in a bit.
Back on the Beam, we’ll add an Animator component and create a new animation for it. When editing the animation, we can actually make changes to child objects, so we can animate the Beam Sprite’s scale, even though the animation in on the Beam. We’ll create an animation with three phases: the setup phase, damage phase, and the shutdown phase.
In this example, only the damage phase is fully opaque to make it clear that it can do damage. The setup phase fades in a bit and scales down to look like it’s focusing power, and the shutdown phase fades out while shrinking down to nothing.
Creating the Beam’s Functionality
Let’s add a 2D box collider on our Beam and adjust it to fit our beam in its damage phase. Now we need to make sure it’s only enabled when it’s in its damage phase. Let’s start by creating a script for our beam and creating some functions to turn the collider on and off.
Now in the beam’s animation, we can use Animation Events to call functions at specific points in the animation! All we need to do is click the Add Event button in the animation window.
When we select an animation event, we can choose what function to call from a dropdown in the Inspector. The listed functions will be from all the scripts attached to the object that has the Animator component. This is why we have the Animator on the base Beam object, so that we can access our functions to use in Animation Events.
Now we can write all the functions we want and set them up at the exact timings we want. Let’s write a function to play an audio clip and another that destroys the beam when it’s done.
Note that only functions that require no arguments can be called in an Animation Event, since there’s no way to provide any arguments. Now we can start our audio at the correct timing and destroy our beam at the end!
As a clarifying point, there are lots of ways to do things in Unity. For example, it is possible to enable and disable the collider in the animation itself. However, when an animation places an object in a state, it cannot be overridden in code, so that state is locked until the animation ends. By using animation events to enable and disable the collider by calling a function, the state of the collider can still be affected by code elsewhere. In other words, if we wanted to disable the collider early, we could only do that by using animation events.
Finally, we’ll detect whether the beam hits the player and do damage if it does. We want to make sure to use On Trigger Enter, so that it’s only called once when hitting the player. We could use On Trigger Stay if we wanted it to damage the player every frame.
And now we have a working beam weapon that charges up, only does damage during a certain window of time, and destroys itself when it’s done!