Phase 2: Creating a Smart Enemy
Objective: Create an Enemy that can detect the Player when it is behind them and fire a laser.
The first thing I will do is create a copy of the Enemy and rename it Enemy_Smart. The next thing is to create a new script and inherent the Enemy script so I can reuse its code.
I will use RayCasthit2D to detect the Player. So I will need a radius and distance variable. Also I will set up a bool to determine if the enemy can fire.
The RayCasthit2D is setup as follows:
When the Player is detected, I will call the FireLaserUp Method
This Method will set the can fire to false and instantiate the laser. It also calls a method in the laser script and starts the cool down.
Now that the Smart Enemy script is all set, I will move to the Laser script.
I added a Method to set the Laser to a SmartLaser. This Method is called when the Laser is instantiated.
Also in the Laser script, I updated the OnTriggerEnter2D Method to detect the SmartLaser.
Let’s see this on action.
Happy Coding!!!