Enemy Avoid Shot
Objective: Create an enemy type that can avoid the player’s weapon. When you fire a shot,the enemy should detect a shot in range and try to avoid it.
The first thing I did was create a new Behavior that inherits from EnemyMovement. I then added the BoxCast code to detect if there is a Player Laser in front of the Enemy. In FixedUpdate if there is a Laser detected I set the target detected to true. I override the SetMoveDirection. If there is no target detected then I call the EnemyMovement SetMoveDirection and return. I then dodge the Player Laser based on the Game Move Direction.
Now I need to determine what value I need to set the move direction based on if the Laser is to the left or right of the Enemy
- Moving Left is -1(Laser position is more to the right)
- Moving Right is 1(Laser position is more to the left)
- Moving Up is 1(Laser position is more Below)
- Moving Down is -1(Laser Position is More Above)
The Enemy can now dodge the Player’s laser. If you time it right you can make the enemy dodge right into one of your lasers.
I am getting an error message, when the enemy can take more then one hit. This is because the detection code is happining in the FixedUpdate Method and I am using the target in the next Update Method. The problem with this is that the Laser has already been destroyed by the time I am trying to acess it’s transform.
To fix this I moved the dodging code to the Fixed Update Method.