VG1, Quest 5 - Space Shooter, PT1

Download Files

Supporting files for VG1 quests are part of a single archive that you can download here.

Create Scene

Create and open a new scene called SpaceShooter. (We will use this scene for both Q5 and Q6.)

Import and Slice Graphics

Import Q5 course files.

For all 4 graphics, set:

For spaaaaace, set Sprite Mode = Multiple and use the Sprite Editor to Slice with Type = Automatic. Within the Sprite Editor, set the Pivot point for the Missile sprites to "Right Center".

For ship[0-2], set Pivot = Right.

Click Apply as prompted.

Create Player Ship

Create a Ship scene object with the following components and settings:

We will discuss the nature of Kinematic physics in class.

Create Projectile Prefab

Create a Projectile scene object with the following components and settings. Prefab it.

From Edit > Project Settings > Physics2D, make sure the Player and PlayerProjectile intersection is unchecked, so that Players cannot collider with their own PlayerProjectiles. We also do not want PlayerProjectiles to hit themselves.

Create SIX Asteroid Prefabs

Create 6 Asteroid scene objects with the following components and settings. Prefab each of them separately.

Create Explosion Prefab

Create an Explosion scene object with the following components and settings. Prefab it.

Create Ship Animation

Create a folder Assets/Animations/Ship/

Open Window > Animation > Animation.

Select the Ship scene object.

Click Create in the Animation window and save the file as Animations/Ship/Thrust.anim

Change Sample Rate to 10 frames per second.

Click Add Property > Sprite Renderer > Sprite.

Select and Delete the Existing Keyframes.

Drag the two different Ship Thrust textures from your Project onto frames 0 and 1 of the Animation timeline.

Create Projectile Animation

Create a Projectile Thrust animation and save it as Assets/Animations/Projectile/Thrust.anim. Use the two different Missile Thrust graphics with the configuration shown below:

Create Explosion Animation

Create an Explosion animation and save it as Assets/Animations/Explosion/Explode.anim. Use spaaaaace graphics 3, 4, 6, 13, 2, and 5 in the configuration shown below:

Inspect your Explode animation by selecting it in your Project assets.

Uncheck "Loop Time".

Clean Up Prefabs

Finalize any updates to your prefabs and REMOVE all prefabs from the scene including the 6 asteroids, projectile, and explosion. (The ship should NOT be a prefab.)

GameController Setup

Create a GameController C# script and attach it to a GameController scene object.

We will use a technique known as Static Instance Referencing to write more efficient code. This technique is similar to the Singleton programming methodology. We will discuss the caveats of this approach in class and will address the shortcomings and benefits over multiple assignments.

This allows us to reference GameController purely through code without having to drag-and-drop into a property field on every other object. Static Instance Referencing ONLY works if there is exactly one of an object. There will always be only one GameController in this scene.

You will see how this makes GameController more quickly accessible in the next step.

Ship Movement

We will create a stopwatch timer within GameController using a public property timeElapsed.

Time.deltaTime accurately represents the passage of time between game frames even if your game lags.

Create a Ship c# script and attach to the Ship.

Using our Static Reference to GameController, we are able to read the timeElapsed from the Ship without creating a Public Outlet.

We will feed the passage of time into a Sine Wave to get an automatic back-and-forth motion for our ship.

Asteroid Movement

Create an Asteroid C# script and attach to your Asteroid prefab.

Spawn Points Setup

Create 9 SpawnPoint gameObjects.

Position them just off screen spread vertically with position spacings {10, 4, 0}, {10, 3, 0}, {10, 2, 0}, {10, 4, 0} ... {10, -4, 0}.

Asteroid Randomization and Spawning Timer

Create Public Outlets for the Asteroid prefabs and where they can potentially spawn.

Fill in these blanks in the inspector. spawnPoints are in your Scene. asteroidPrefabs must be Prefabs from your Library.

Create a function for spawning a random Asteroid Prefab at a Random Spawn Point.

To put this on a timer, we need a property to keep track of the delay between asteroid spawnings. We will also have a configurable minimum and maximum delay.

We will dynamically decrease this delay as time progresses. There is no universal equation for this computation. It needs to be playtested for "game feel." Different games use unique difficulty curves.

We will create a Coroutine using an IEnumerator to act as a delay timer between asteroid spawns. Coroutines ARE NOT true multithreading or background threads, but are a technique for setting up "Side Events" in addition to Unity's main loop.

This Coroutine calls itself at the end of its own execution, creating a repeating timer. IEnumerators MUST have a yield in order to prevent them from becoming an infinite loop that freezes your game.

Although our coroutine loop repeats itself, we must start it in the first place. We want to start spawning asteroids at the Start of our game:

Ship Firing Logic

Our ship firing logic is very similar to the asteroid spawning because both are automated.

We start by creating a Public Outlet for the projectile prefab we want to shoot, and creating a firingDelay property so we can dynamically change the frequency of our shooting.

The projectile prefab must be assigned in the property field.

We create a FireProjectile function to call whenever we want our ship to shoot.

A Coroutine built using an IEnumerator yields for the appropriate delay time, fires a projectile, and then starts the timer all over again.

The Timer still has to be started in the first place.

Explosion Prep

We want to use Explosions throughout our game, so we will keep a reference to our Explosion prefab in the GameController where it can be easily references by everything else in the game.

Public Outlets must have their blanks filled.

Projectile Logic

Create a C# script for the projectile and attach it to the prefab.

We will start off with a standard Rigidbody2D Reference Outlet. We will also create a target property for keeping track of our projectile’s target.

A ChooseNearestTarget function will look at every asteroid in the scene and choose a target that meets the following criteria:

  1. It is to the right of the projectile because we don’t want to target asteroids behind the player.
  2. It is the closest asteroid of all the ones that we have checked.

We will set up two variables for acceleration and maxSpeed. We will make these dynamic in a future step.

Every frame of our game, we will point the projectile toward our target.

Our projectile will continually thrust forward (which happens to be towards the right based on how our sprite was drawn) clamped by the maxSpeed set in our variables.

Last, on collision, we check if we have impacted an asteroid. Our projectile will destroy the asteroid, destroy itself, and create an explosion where the asteroid was. That explosion game object will destroy itself soon after.

We will program the actual interactivity of our game in the next assignment. The version of the game you've created up to this point can still be "played." You will see should see that the ship automatically flies through an increasingly dense asteroid belt. It regularly fires missiles which chase the asteroids and create explosions on impact.

Save and Test

Playtest to ensure all interactions work as expected and that the addition of any new features hasn’t broken any earlier interactions.

Submit Assignment

SAVE any open files or scenes.

Submit your assignment for grading following the instructions supplied for your particular classroom.