Day 52: Enemy likes to run into Player
Objective: Player gets within a distance of enemy, enemy tries to ram the player.
For this enemy we will need to create a new sub class of enemy, this time it’ll be called AggressiveType.
In this script we will need a float we can change to get the perfect distance. As well as a Quaternion to save the starting rotation.
We will need to override the start function to save the start rotation. But we still need to use the base Start function so we’ll use base.Start() as well.
Next is to override the CalculateMovement Function. This is to change the movement of this enemy. Inside this function, the first thing to do is make sure the enemy is not null before we continue. We will then save a local variable call distance and use Vector2.distance(players postion, enemy position).
Next will be to check the distance and if the distance is less than the specified distance rotate toward the player and move towards its.
If the distance is greater than the specifiedDistance set the rotation back to the start rotation and use the base.CalculateMovement(), to continue traveling the same.
That’s it for the logic. What we will need to do next is to set up the enemy. BoxCollider2D with trigger checked, RigidBody2D with gravity set to 0, animator and the AggressiveType script.
On the script component adjust everything how you would like and save as a prefab. Last thing to do is add to the spawn manager. After that we are done! This enemy will now trying to ram it’s self into the player!
Thank you for reading!