Now that I have some art assets for my new boss Anglerfish, my objective for this article is to bring the boss into the Unity-verse and get it battle ready.
After setting the Texture Type on the new PNG assets to be Sprite (2D and UI), I drag the Anglerfish body and jaw assets into the scene view.
Both sprites are automatically centered to the screen, so I need to change the position of the jaw asset, and assign it’s Sorting Layer to be 1, so it appears on top of the body.
That looks much better already!
I want to rotate the jaw asset to open, so for this to look correct, I need to change the pivot point on the sprite. By default, sprites will rotate from their center position. The Sprite Editor can be downloaded from the Unity’s Package Manager, and then the pivot point can be moved with a simple click and drag of the mouse.
In the Hierarchy window, I drag the jaw asset into the body asset to make it a child. Now it will move in world space with the parent body object.
After selecting the anglerfish jaw asset, I open the animation window and start a new animation. Properties for position and rotation are added for the transform of the object. The initial keyframes hold the starting position. The jaw drops open, increasing speed as it falls, and then bounces a little to get a feel for some weight. Yes, I am ignoring the fact that this is underwater, so the weight issue is not really an issue. I’m just having fun animating! Loop Time is turned off, being I only want the animation to play one time when triggered.
Now that there is one animation on this jaw game object, I will need to add an additional animation by clicking the drop-down tab on my current jaw open animation, and selecting create new clip.
After creating an animation to close the jaw, I open the animator window to make some transitions. I right-click to create a new state, rename it to default, then right-click the new state to set it as the default state. This will redirect the entry transition to the default state. From the default state I add a transition to the jaw animation, and from there a transition to the jaw closed animation is added. The final transition moves back to the default state. The Parameters Tab in the upper left is used to add two conditions of type Trigger. One is for opening the jaw and the other is to close it.
After selecting the transition from the default state to the jaw open animation, I set the condition to require the JawOpen trigger. The transition from the jaw open animation to the jaw closed animation gets the condition of the JawClosed trigger. These can now be called through code when I want to trigger them!
Here I manually click the triggers in the animator to test them out while the game is running.
Two more quick assets are created to accompany the Anglerfish into glorious battle. The one on the left will serve as an indicator to tell the player where the weak spot on the boss is. The asset on the right will become some additional light on the anglerfish’s lure.
The weak spot indicator gets a simple animation that changes it’s size. It will start large and then decrease in size for a looping animation.
Once the jaw is open, the indicator will appear to show the player where to shoot in the open mouth to hurt the boss. This will be the only way to deal this tank any damage!
The light asset is positioned and it’s sorting layer is increased to ensure it appears above any other sprites in the scene view.
I should mention that these are all individual game objects that have been made children of the parent body object. I now add a new empty object to serve as the target for the player to hit, and it get’s a CapsuleCollider2D component added.
The light game object gets a CircleCollider2D.
The main body gets a PolygonCollider2D, and I make sure to adjust the lower front region so that the target collider is exposed.
The jaw asset also gets a PolygonCollider2D. These are great for auto-conforming to the shape of an object.
I thought it would be best to have all of these object become prefabs, so I make prefabs of the children objects and then make another from the parent object.
Now to deal with multiple colliders on one object, which can be a pain. The problem I was having while trying to make detections with the children and parent colliders, is that the children colliders also trigger the parent when collided with. This is normal in Unity and is called compound colliders, but it does pose a challenge when you want different colliders on one object to do different things and trigger independently. I was advised to take the opposite route, using the player projectile script to detect the different colliders on the boss, so that’s the route I am taking. I want the player projectile to check for different tags on collision, so I make new tags for the different components. The body and jaw get a Tag of Anglerfish, the light gets a Tag of AnglerLight and the target gets a Tag of AnglerTarget.
Here in the projectile class, the OnTriggerEnter2D method checks for the new tags on the boss and sends debug messages accordingly before destroying itself.
A few well placed shots from the player tell me that everything is working and it’s time to dive into some script communication. I hope to see you in my next article where I continue to bring the new boss to life. Thanks for reading!