-
-
Notifications
You must be signed in to change notification settings - Fork 32
Home
- Where can I download the sprites?
- Why not use colliders to detect when invaders hit the edge of the screen?
- Why can I not drag my prefabs into the array in the editor?
- How do you handle bunker destruction?
- How do you implement the mystery ship?
- Collisions are not working between the invaders and the player
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.
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.
-
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.
-
To check for collision with the colliders, we would use
OnTriggerEnter2D
. We could handle this as part of theInvaders.cs
script or in theInvader.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
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.
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:
- Bunker: https://github.com/zigurous/unity-space-invaders-tutorial/blob/main/Assets/Scripts/Bunker.cs#L7-L125
- Projectile: https://github.com/zigurous/unity-space-invaders-tutorial/blob/main/Assets/Scripts/Projectile.cs#L28-L45
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
I regrettably forgot to include one clip in the video showing me add a Rigidbody2D
component to the "Invader_Base" prefab. In order for collision to occur between two objects, they both need colliders, and at least one of them needs a rigidbody. This means you could also add the rigidbody component to the player. In either case, set the Body Type
property on the rigidbody to Kinematic
. This is because we don't want to simulate the physics of the object, we only care about collision detection.