Now that I have a boss with working components, my objective for this article is to give it the ability to deal damage to the player. The journey begins briefly in Corel Draw, where I whip up a fast projectile for the lantern on the anglerfish boss to shoot.
After exporting a fresh PNG for the projectile, I bring it into Unity by assigning the Texture Type of the asset to be Sprite (2D and UI), dragging it into the scene view and giving it a name. For a visual appearance that sits above the player and the boss, it’s Order in Layer value is set to 2.
Components of types Circle Collider and Rigidbody 2D are added to the projectile game object. Is Trigger is set to be true on the collider and the rigidbody has it’s gravity scale set to 0.
Lastly, it gets a script component called “Tusk” that I use as a shared script for all of the various projectile behaviors. Setting up the behavior here is really easy, being that the green piranha enemy type already shoots a projectile that has the exact behavior I am looking for. In void start, the projectile finds the Player by it’s Tag, and then stores it’s position one time as a direction to travel. The projectile then moves forward in the last known position of the player.
More on that behavior and the shared script here. https://jaredamlin.medium.com/challenge-create-a-smart-enemy-type-7222646e81f4
The mentioned projectile has an ID of 3, so I assign the new projectile this boss will be shooting an ID of 3 as well.
Now that the lantern shot projectile is ready to go, I drag it into the prefabs folder.
The script on the lantern game object gets two new variables. One variable stores the Player class, which I will null check before firing projectiles. Another with a Serialized Field attribute stores the newly acquired lantern shot game object prefab. In void start, the player is found using it’s Tag, and then null checked for life. A coroutine is then started to begin the firing method on the lantern.
After saving in Visual Studio, I return to Unity to assign the lantern shot to the lantern game object by dragging and dropping the prefab into the inspector on the script component.
Back in the firing method coroutine on the lantern object, a while loop is entered to fire the projectile. I have a previously existing bool that checks if the jaw on the Anglerfish is open, so I use this as a condition for the projectile to fire in the while loop. Once the player temporarily disables the lantern after hitting it three times, the jaw will open, the bool will set to true, and the while loop will exit. I also ended up introducing a new variable at the top of the script to store the time to wait between firing projectiles (lanternFireRate), which is currently every 1 second.
Here is a clip the boss firing at the player, causing damage and then pausing once the lantern life is depleted!
Aside from the lantern shooting light projectiles at the player, I need to attend to the expected and obvious damage from the body of the Anglerfish. The script on the body needs a handle to the player class, which is assigned in void start via GameObject.Find. The OnTriggerEnter2D method looks for the tag of “Player”, and calls the damage method on the player if the condition is met.
Now the player takes damage when running into the boss!
I want to use that big body to my advantage through some kind of dash attack, and with that attack comes chomping teeth. I select the Anglerfish Jaw object in the Hierarchy and create a new clip in the Animation window.
After giving the new animation a name, attributes for position and rotation are added to the animation timeline. A few tweaks give me this looping chomping teeth animation, which I want to trigger when the boss does a forward dash.
I want to enter this animation from the default state, so I right-click on it and create a new transition to the chomping animation state. Another state is create to return to default. On the left, a Parameter of type bool is created for IsChomping.
If IsChomping is true, the initial transition into the chomping animation will trigger.
Conversely, if IsChomping is false, the animation will return to the default state.
Here you can see me toggle the bool from true to false, and the chomping teeth respond accordingly!
I hope you join me in my next article where I finally get this boss enemy moving!