Let’s go over the implementation of enemies that will dodge the projectile.
In this article I will combine the distance checking code from the homing projectile article with the reverse of the absorption code from this article. You will need to have created the Enemy script from as shown in this article.
Open up the Enemy script and add the function for detecting the projectile. If more than one projectile is in the scene, then select the closest one as the “target” to avoid:
Add another value to the the EnemyType enum for this new type of enemy:
We will check the distance between the enemy and the projectile inside the movement function. If the projectile is too close to the enemy, then we move the enemy away from the projectile object. We use the reversed Vector2.MoveTowards to accomplish this:
Ensure that the projectile has the tag of Projectile: