Framework 2: Boss Ai (part 3)
You know, the more I add to this boss character, the more I feel like I am creating a monster that I can never personally defeat…
Now that we have all of the desired states and behaviors down, it is time to link everything together via the Boss Behavior Script which we started back in part 1 of this tutorial.
The Boss Behavior Script
We are first going to create a handle and then create the reference to the Animator that houses our states and behaviors within the start method.
Now its time to set up the Triggers. An example of what I mean is, lets make it so my Boss would transition to the second phase where it has new attack patterns and becomes more aggressive when his health is below 50%. I also made a destruction sequence when health is at zero.
So in void Update, I used an IF statement where if health is below 50%, I tell the animator to trigger “Stage 2”. Also within this script I have some spikes to change the looks of the boss, so I set that active as well during this stage.
In the inspector, since I want to have a smooth transition between both phase A and phase B going to Stage 2. I unselect the “Has Exit Time” within the Inspector when I highlight the white line arrows connecting the two states. I also made the Transition Duration to 1 second to prevent any sudden snapping of animation.
Back in the Boss Behavior script, I create a new public method call “Boss Start” where I trigger the Start parameter to begin the boss sequence.
Trigger Boss Spawn Script
All we need now is a Trigger to get the boss to spawn. For this sequence, I decided to use an empty game Object with an box collider 2D, and place it near the end of the level where I want the boss to spawn.
In the script, I create an handle for the Boss Behavior script which I am going to call the animator from. Then call the reference in the Start method.
I create an new private void On Trigger Enter 2D method and within in I use an IF statement to check if the colliding game object has the tag “Player”. When it does, I will call the animator from the Boss Behavior script and trigger the start method which had set to “Boss Start”.
Save, hit play and watch your newly created boss come to life!