Space Shooter Challenge: Enemy Avoid Shot

Today’s challenge was to create a new enemy type that could avoid the Player’s projectiles.Whilst exploring options for other enemies, I found one that I considered replacing the original enemies with. Instead, I decided I would have this model as an a…


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee

Today’s challenge was to create a new enemy type that could avoid the Player’s projectiles.

Whilst exploring options for other enemies, I found one that I considered replacing the original enemies with. Instead, I decided I would have this model as an additional enemy type, but with the similar design of three different iterations in terms of how they travel. Straight down, Diagonally Left, and Diagonally Right.

To keep my code easy to read, I wanted to pair these two enemy types together, which meant a bit of reshuffling of ID’s for the advanced enemies I had previously created.

The above image shows a snippet of the IDBehaviors Switch Statement, with cases 3, 4, and 5 looking after this new Dodge Enemy. They all run the FireLaserCoroutine, with the difference being their rotation. The SpawnManager looks after their spawn positions, which follow the variations on the original enemy.

Additionally, I created a Coroutine for detecting lasers. In this, I wanted to run a while loop to make use of a RayCast searching for other Colliders. I needed to add an offset so it wouldn’t simply detect itself, and I also opted for CircleCast so I could have the detection cover the width of the object. Otherwise, the Laser’s would only be detected if they happened to be right in the exact middle of the enemy object.

Next I checked the Collider’s tag, before calling a Dodge method. I made use of a bool so that this method would only be called once.

I then decided that I wanted the enemy to be able to Dodge again rather than just once. So I enclosed the whole thing in another while loop that runs essentially while the Enemy is active.

In the Dodge method I wanted to pick a random direction to Barrel Roll. To do this, I had rotated the enemy in Blender and rendered a bunch of sprites into a rolling animation.

Depending on the direction selected, I could call a BarrelRoll Coroutine, passing in the appropriate direction value.

This BarrelRoll Coroutine was essentially the MissileEnemy routine repurposed into being used for both enemy types. I needed to take the directionToMove int, and set the Vector3 variable to the appropriate direction, whilst also setting an Animation Trigger.

Then, I could get the length of the current animation, and run a while loop while this occurred. In this loop, I used transform.Translate, to move the Enemy in the direction that coincides with the animation, resulting in what looks like a barrel roll.


Space Shooter Challenge: Enemy Avoid Shot was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee


Print Share Comment Cite Upload Translate Updates
APA

Calum Slee | Sciencx (2021-06-29T00:26:19+00:00) Space Shooter Challenge: Enemy Avoid Shot. Retrieved from https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/

MLA
" » Space Shooter Challenge: Enemy Avoid Shot." Calum Slee | Sciencx - Tuesday June 29, 2021, https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/
HARVARD
Calum Slee | Sciencx Tuesday June 29, 2021 » Space Shooter Challenge: Enemy Avoid Shot., viewed ,<https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/>
VANCOUVER
Calum Slee | Sciencx - » Space Shooter Challenge: Enemy Avoid Shot. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/
CHICAGO
" » Space Shooter Challenge: Enemy Avoid Shot." Calum Slee | Sciencx - Accessed . https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/
IEEE
" » Space Shooter Challenge: Enemy Avoid Shot." Calum Slee | Sciencx [Online]. Available: https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/. [Accessed: ]
rf:citation
» Space Shooter Challenge: Enemy Avoid Shot | Calum Slee | Sciencx | https://www.scien.cx/2021/06/29/space-shooter-challenge-enemy-avoid-shot/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.