Objective: Make the enemies desperate, when they get within a set range of the player have them charged towards the player.
This tutorial is about adding desperate actions to the enemies. We want the enemy to charge the player once they get within a set distance to the player. This will add more pressure to the player and force them to stay mobile.
To begin we need to create a Vecctor3 variable to reference the player’s position.
We start with this reference set to zero, however we will quickly change those values.
We also need two bools; one for if the enemy is dive bombing, and the other for when it arrives at its destination.
Finally, we need a ramming speed and a variable for the distance in which the dive bomb should be initiated.
We need to create a method to calculate if the player is close enough to initiate the Kamikaze attack. We need to begin with calculating the distance between the player and the enemy.
With this being calculated we can create an if statement for if the enemy is not engaged in the kamikaze attack, and if the distance is below a set distance. My variable for distance is set to 5.
This will have the enemy quickly charge towards the player however, now once the enemy arrives at the players last set location it will now sit at the location and not know what to do once it is there. We now need to go into the calculate movement method.
We need to set an if statement for if the Kamikaze is initiated and the enemy has not arrived yet, then the enemy will move towards the player position, at the ram speed multiplied by time.
Now finish with an else if statement for if the Kamikaze is not initiated or if it is and the enemy has arrived then the enemy will continue to move in its set direction.
Now we need a local float to calculate the distance of the enemy and the player position. With an if statement we calculate that when the distance between the two are less than 0.1f then the arrived bool is set to true.
To finish off the code we need to call the Ram Player method, we want to call this method at the top of the update method. Now every frame the ram player method will be checked and activated when the requirements we’ve set have been met. Now the player needs to stay nimble if they hope to survive! As always, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial.