Phase 2: Aggressive Enemy
Objective: Update an ememy so it attempts to ram the player when it’s close.
For this objective, we will use the original enemy only. First we run an if statement to check if the player is not null and the enemy isn’t the red enemy.
We need to find the distance between the enemy and the player so we use the Vector3.distance to find the distance between the the two transforms and assign it to a float. We don’t want the enemy to be holding up the flow so this will only be active when the enemy is higher or on the same point of the Y-axis as the player. Create two floats, one for each of the transform.position.y points. We also create a Vector3 for each of their full positions.
Next create a quaternion for the target rotation between the enemy and the player using the Quaternion.LookRotation function. We did run an if statement to check if the distance between the enemy and the player is less than a specified float and that the enemies y position is higher or equal to the players. If true we set the enemies rotation using a quaternion slerp which allows the enemy to change rotation to match the target rotation at 1.5 times normal speed by Time.deltaTime. This is where you need to hope you havn’t used all your thrusters as they will be needed to escape. Once out of reach the enemies rotation will be set back to its original rotation using the quaternion slerp again using the Quaternion.identity instead of the target rotation.
In the next post, we will set up a player magnetic field to pull the collectables towards them when triggered.