We continued learning more about the potential topics that will be on the programming exam; today was all about looking into how to incorporate projectiles into our scenarios. We were shown a couple of different ways we could instantiate the bullets in Unity:
- Have a bullet fire when a button is pressed by the player.
- Have a series of bullets fire when a button is pressed by the player, fired at angles based on a circle (so if there are 10 bullets, based on 360º we would have a bullet 36º)
- Have a bullet auto-fired, with a timed pause in between the shots fired.
We decided to implement this mechanic in our game. First, it was a matter of creating a new collider that could be a trigger on the guards – Marc sorted this for us, causing a + sign around the guards where they would be able to see the player if they’re on either side, in front, or behind the guard.
Above, you can see the original green polygon collider around the guard, the new plus-shaped collider for if the player enters the guard’s general area, and the guard’s waypoint path. Now, there a likely more complicated ways to do this with a cicular collider and seeker projectiles, but we’re learning about the latter next week so for now we thought (for simplicity’s sake) this way still worked.
After that, we needed to attach a script so the guards would know what to do if the player enters their “danger zone”!
We took parts from the second projectile script Ant provided us – we thought this would cover the four directions until we learn something for a projectile seeker script. The number of shots was actually changed at the component level to be 4, even though it shows 10 in the script. This particular script created a new variable within the Shoot function, which says the angle of each shot should be a calculation of 360º divided by the number of shots fired (in our case of 4, each shot will be sent out at 90º).
After that, it establishes another variable of the current angle to start at (0), then says that if the current angle is less that 360º, a copy of the projectile will be created (instantiated) in the game based on the current position of the object the script is attached to. It’s then fired and a rotation happens based on the current angle and how much each shot should be angled (anglePerShot)… whew! That was a slightly long-winded explanation, however the script works very well for things like explosions or enemies that have the ability to shoot in all directions.
The nice part is, this also uses a while loop, which is what we were asked to look into last week. Rather than an if statement, which takes into consideration IF something is true, a while loop continually verifies that WHILE something is true, it will keep going until that something become false. So, until our current angle is greater than 360º, the script within will keep running. This is where I mentioned in my research post that you could enter an infinite loop if you’re not careful about your criteria.
Finally, we added a function for when the player (who actually has to have a tag of “Player” for the script to work and avoids any cross-over from other collider enter scripts) enters the “danger zone” trigger, the guard will shoot. In the future, this can be changed for a heat seeker projectile and maybe have it so the player can shoot back to destroy the guard projectiles to protect himself.
There were a couple of other things we decided to add from the last post I made about this game. We’ve now included very simplistic start and game over screens, since we were doing some scene management (and now have two levels). We also wanted there to be a way to let the player know they needed to go through the doorways once all the flowers on the level had been collected. For this, we just added a little extra script that changes the sprite.renderer of the door once the flowers reach less than one.
I think the game is looking quite cute, incorporates all the requirements to practice for the exam, and I’m liking what we’re learning. I’m looking forward to adding more soon!