Adding the final details
We have two things missing here: the enemy is not shooting any bullets, and it doesn’t have animations. Let’s start with fixing the shooting by doing the following:
- Add a
bulletPrefab
field of theGameObject
type to ourEnemyFSM
script and afloat
field calledfireRate
. - Create a function called
Shoot
and call it insideAttackBase
andAttackPlayer
:
Figure 9.67: Shooting function calls
- In the
Shoot
function, put similar code as that used in thePlayerShooting
script to shoot bullets at a specific fire rate, as in Figure 9.68. Remember to set the Enemy layer in your Enemy Prefab, if you didn’t before, to prevent the bullet from damaging the enemy itself. You might also want to raise the AI GameObject position a little bit to shoot bullets from a position other than the ground or, better, add ashootPoint
transform field and create an empty object in the enemy to use as a spawn position. If you do...