2D Galaxy Shooter P2: An Enemy can Avoid Shots!
For today’s challenge, I implemented an enemy type that can shoot and juke player projectiles!
The more features implemented, the more challenging the game is getting! This new enemy type adds an extra level of difficulty. We need to be smart when shooting, and accuracy is something to strive for to save ammunition.
This new enemy type avoids the player’s projectile when in front. So we must spray our shots to land a hit.
Since we are dealing with a new type of enemy, I did a quick photo-bash of the original enemy sprite given by Game Dev HQ to something new. All I did was duplicate the original, move them around, erase certain parts, and repeat.
This is the result!
I then set up the enemy in Unity. Because it needs to detect a player’s projectile, I added a trigger collider, with a new layer selected, in front long enough to detect and move to the side.
I created a new script for the trigger box collider above. I called it “DetectProjectile.” That is all the script will do. So I want to use the
private void OnTriggerEnter2D(Collider2D other)
}private void OnTriggerExit2D(Collider2D other)
When the projectile enters the trigger box collider, I call and set the
isSwitching bool to true. It will allow me to execute a condition in the enemy movement script. I also want the enemy to change its movement type to juke. It will make the enemy move to the side.
For the OnTriggerExit2D(), when the projectile exits from the box collider, I want the
isSwitching bool false. I also need the enemy to change its movement type back to default.
In the enemy movement script, I added a new type of movement called juke:
if(_enemyMovementType == EnemyMovementType.Juke)
if(randomSideJuke == 1)
transform.Translate((Vector3.right + Vector3.down) * (_speed + 1) * Time.deltaTime);
else if(randomSideJuke == 2)
transform.Translate((Vector3.left + Vector3.down) * (_speed + 1) * Time.deltaTime);
I have a random variable that determines the side on which the enemy will juke to. If
isSwitching turns true, then we want the enemy to juke either to the left or the right!
We now have an enemy that can avoid projectiles. They are tricky to hit but doable.
That is all for today! Thank you for your time!