Skip to content
Adam Graham edited this page Jan 31, 2022 · 12 revisions

Links

FAQs


Where can I download the sprites?

The sprites are included the full project / source code which can be downloaded with this link: https://github.com/zigurous/unity-space-invaders-tutorial/archive/refs/heads/main.zip

Download and unzip the project, then look for the sprites in the Assets/Sprites folder.


Why not use colliders to detect when invaders hit the edge of the screen?

We could use colliders to detect when invaders hit the edge of the screen, but this solution would be more complex for two main reasons.

  1. The edge of the screen is going to be different depending on the player's screen resolution. This means we would need to write additional code to change the position of the colliders depending on the screen size. This code isn't too complicated, but it's still a bunch of excess code we can avoid.

  2. To check for collision with the colliders, we would use OnTriggerEnter2D. We could handle this as part of the Invaders.cs script or in the Invader.cs script. Either way, it'll require more complex code. For the former, you need to make sure the invaders' collider is always positioned and sized correctly to match the grid anytime an invader is killed. For the later, you would need to implement a design pattern to communicate from an individual invader to the group as a whole.

You might think using colliders prevents needing much code at all, and in most cases that's true, but for this particular use case it ends up require more code and code that is more complex. Although we took a manual approach of checking when invaders hit the edge of the screen using custom code, it ends up being a lot simpler.

Here is the relevant code for the solution used in the video: https://github.com/zigurous/unity-space-invaders-tutorial/blob/main/Assets/Scripts/Invaders.cs#L89-L114


Why can I not drag my prefabs into the array in the editor?

At around 25:20 in the video, I drag my prefabs into the array on the Invaders component. If you are unable to do this, it is most likely because the Invader.cs script is missing from your prefabs. When we declare that array of prefabs in code, we specifically declare it as an array of Invader, as seen here:

public Invader[] prefabs = new Invader[5];

This means the code is expecting each of the game objects to have the Invader.cs script attached to it. The Unity editor is smart enough to know this, so it prevents you from assigning any game object to the array that is missing the script.


How do you handle bunker destruction?

I intentionally did not include this in the video because it's pretty complex and would have taken a lot of time to implement. I did, however, include this functionality in the full project available here on GitHub.

Most of the logic exists in the Bunker.cs script and a little bit in the Projectile.cs script. To destroy the bunker, we use a "splat" texture and overlap it on the bunker. For every pixel that is shared across both the bunker and the "splat" texture, that pixel becomes transparent, thus creating holes.

Whenever a projectile enters the trigger area of a bunker, we have to check for collision manually by seeing if the pixel that was hit is transparent or not. If the pixel is transparent, the projectile continues moving through, otherwise it is destroyed and causes a "splat" on the bunker.

Relevant source code:


How do you implement the mystery ship?

I did not include the functionality for the mystery ship in the video as I did not want the video to be any longer than it already is. However, I have included the source code for this in the full project available here on GitHub.

The mystery ship keeps track of its direction and moves left or right accordingly inside the Update function. It checks to see if it hits the edge of the screen (just like the invaders) and then "despawns". We use Unity's Invoke function to respawn it after 30 seconds. This cycle continues on repeat.

See the MysteryShip.cs script here: https://github.com/zigurous/unity-space-invaders-tutorial/blob/main/Assets/Scripts/MysteryShip.cs

Clone this wiki locally