My Work on Toy Takeover

My Work on the Game

For toy takeover, I wrote a whole series of scripts in Unity in C# I did this on my own as I was the sole programmer.

The scripts for this game were separated into 5 main categories: Player Scripts, Enemy Scripts, Game Management, Items & Projectiles, GUI Scripts and scriptable objects.

Player Scripts

This folder includes everything to do with the character controller including character movement, health and damage, use of the weapon and its ammo and sound effects that the player-controlled character makes.

View folder On Github

Player Controller

This file contains all stats for the player including health, armour, fuel and the field drain speed when the payer takes off in the air on the jetpack. An array of game objects is also used to store each weapon that the player has. The spawn point that the player respawns at after going off the edge of the level along with the damage taken is also stored here.

View Source File


Player Movement

This script is used to move the character around in all directions using the keyboard as well as rotate the camera with the use of the mouse. A game controller can also be used for movement.

There are variables to control the movement speed, how high the player can jump, how fast the fall and how fat they get knocked back when colliding with an enemy.

The vertical and horizontal speeds for the camera are also adjustable along with how far up or down you can look before the camera stops rotating vertically.

The jetpack allows you to rise up off the level by consuming fuel and variables for the starting fuel and max fuel is stored here. There are also variables used to control the physics of the jetpack including lift, thrust and weight.

View Source File


Player Sound

This script contains variables for all the different audio clips that the player game object produces. There are sound effects for player movement, taking damage and when the game or wave ends.

The sound effects are triggered by other scripts by invoking one of the public PlaySound() functions in the PlayerSound script. There are also functions to stop playing a specific sound or stop all audio sources attached to the player.

View Source File


Weapon Controller

The WeaponsController script takes a scriptable object for the type of weapon it is using. Weapon stats such as the maximum ammo, starting ammo and damage dealt are all handled here. There all also different functions that are invoked depending on whether the weapon is a projectile weapon that spawns projectiles, or a laser weapon that uses a raycast to check whether the shots hit an enemy.

A transform for the firing point along with the visual effect played when firing is also assigned to this script.

Every time a shot is fired one of the sound effects in the FireSounds array assigned to that weapon gets selected at random and then played.

View Source File


Enemy Scripts

This folder handles everything to do with the enemy in the game that damages you. It includes different scripts for different types of enemies including melee enemies, ranged enemies and dolly cart enemies. It also handled enemy patrolling A.I. and all the sound effects that the enemies produce.

View Folder on Github

Enemy Controller

This is the main script that is attached to all enemy prefabs that spawn in the level. There are variables that for setting its maximum health along with the amount of damage dealt to the player from each hit. Particle effects are also played whenever the enemy takes damage and another particle effect is played when the enemy Is destroyed.

The enemy also deals damage to the player every x amount of frames whenever the player is colliding with it.

The animation speed of the enemy for movement attacking and taking damage can also be adjusted on this component. Setting the enemy as an environmental hazard will destroy the enemy as soon as it comes into contact with the player.

The enemies in this game all use a finite-state machine controlled by an enumerator. There are 7 different states that the enemy can be in including Idle, Patrol, Follow, Aim, Attack, Damage and Dead. Switching the state will change the animation on this script but on other scripts attached to the object it will change the behaviour of the enemy. Whenever the enemy takes damage, this script will switch it to the damage state.

View Source File



Enemy Sound

This script stores all the sound effects that are assigned to each enemy as well as plays them and the script is attached to every enemy prefab.

There is a series of sound effects for whenever the enemy attacks, takes damage or dies. Whenever one of these events occurs, a sound effect will get chosen randomly from the matching array and then played.

Like the PlayerSound script these events are triggered from other scripts and will invoke the public functions on EnemySound.

View Source File


Patrol Enemy AI

This script is attached to all enemies that patrol the level through patrol routes when after spawning and is attached to both the melee enemy (Grunt) and ranged enemy (Robot).

As soon as the enemy spawns it will retrieve the patrol route from the patrol manager and then move towards then head towards the first waypoint using the NavMesh agent. After reaching a waypoint it will then move to the next and then the next until it has reached the final one and then head back to the first waypoint. This only continues if the enemy is in the Patrol state.

The enemy can also switch from patrol state to follow state in this script which is done one of two ways: The player enters the enemy vision while the "Use Vison Cone" checkbox is checked or the player is within a certain radius from the enemy if "Following When in Range" is turned on. There are variables to adjust the length and the width of the vision cone used for enemy vision and the radius can also be adjusted using the "Min Distance From Player" variable.

The waypoint information gets updated at runtime and is only used for debugging purposes.

View Source File


Melee Enemy AI

This script is attached to melee enemies which in game this the "Grunt" prefab. While the enemy is in Follow state, the enemy will move towards wherever the player is standing using the NavMesh Agent until it is in attack range. When the enemy is in attack range it will switch to Attack state where it will stop moving and damage the player once at every time interval which is 1 second divided by the attack rate. If the player goes out of attack range the enemy will switch back to Follow state.

Adjustable variables for the attack range and attack rate have been included.

View Source File


Range Enemy AI

This script is attached to ranged enemies which in this game is the "Robot" enemy. While the enemy is in follow state it will move towards the player using the Navmesh agent. Then each frame will check that the player is still within the view distance and there is not a wall or another object obscuring its vision. If both of these conditions are true the enemy will switch to Aim State.

While in aim the enemy will rotate it is gun until it is facing the player. When it is it will switch to Attack state. If the player goes out of range or moves behind a wall while the enemy is aiming then it will switch back to Follow State.

While in the Attack state, the enemy fire projectiles at the player. This will continue until the gun is no longer pointing at the player in which case it will switch back to the Aim state.

The view distance and the turning speed of the gun can be adjusted using variables in this script. There is also a particle system that plays every time the enemy fires.

View Source File


Dolly Cart Enemy

This script is attached to enemies that use Unity's Cinamachine Dolly Cart to move around the level. In this game, this is the car prefab. When colliding with the player it will deal damage equal to the value set to the Collision Damage variable. If Destroy on collision is checked the game object will also be destroyed when colliding with the player or an enemy. Destruction Particles are also assigned which play as soon as the game object is destroyed.

View Source File


Game Management

This folder contains the GameManager and PatrolManager scripts that are attached to the Game Manager game object in the Unity Scene. It also contains the spawner scripts that get attached to the spawn point for cars and item pickups for when they respawn after being removed from the scene.

View Folder on Github


This is the main Game Manager that stores the game state in an enumerator (Standby, Running Paused, Win, Loss), as well as the wave number, number of enemies left and the time elapsed while playing the game.

The different types of enemies to be spawned during enemy waves are stored in the enemies array and the spawn points that enemies can be spawned at are placed in the Spawn Points array. The dolly cart transform in each car game object is assigned to the dolly carts array so that they can respawn at their appropriate location after they are destroyed. The enemy waves come in the form of scriptable objects which are assigned to the enemy waves array. The first-person camera attached to the player game object is also assigned to the Game Manager.

When the level begins the Game Manager will spawn item pickups at all of the item pickup locations. It will then check the enemy spawns each frame to check if enough time has elapsed for the next enemy in the wave and if it has then been, then it will spawn the assigned enemy at the assigned spawn point through the EnemySpawn class contained in the scriptable object.

Whenever an enemy is destroyed it will invoke the KillEnemy() function which subtracts the enemiesLeft variable by 1. If the value of enemies left equals zero then the next wave will begin. When this happens the item pickups all get spawned again at their allocated location and enemies will start spawned using data on the next element in the Enemy Wave array.

If all enemy waves are completed then the game state will change to Win. If the player runs out of health the game state will change to Lose. When a wave is completed the game will change to Standby state until the next wave begins. If the Esc key is pressed it will change to Paused state. All of these states will show different information on the user interface and the rest of the time the game is in Running state.

View Source File


Patrol Manager

This script is where all of the patrol routes are set up. The number of patrol routes is defined followed by the number of waypoints in the patrol route. From there transforms are placed at each element for each waypoint.

Whenever a grunt or robot enemy is spawned it invokes the GetPatrolRoute() function on the PatrolManager Script which selects one of the patrol routes at random and returns it. If the two-way box is checked the enemy moves back in reverse order after reaching the last waypoint instead of cycling back to the first waypoint. If the Randomize Order box is checked then the waypoints get shuffled and the patrol route is returned with the waypoints being set in random order.

View Source File


Item Pickup Spawner

This is a script that gets attached to every transform in the game that is an item pickup spawn point. One or more item prefabs are assigned to the script and if the "Spawn Random Pickup" variable is set to true then it will spawn a random one from the array. If it is set to false then it will spawn the element equivalent to the wave number unless there are fewer items than waves in which case it will spawn the last time in the array.

There is also a spawn angle variable that sets the direction the item pickup faces after being spawned.

View Source File


Items & Projectiles

This folder contains the scripts for the Item controller and the projectile controller. The ItemController script is attached to all item pickups in the level while the ProjectileController is attached to all projectiles that spawn when fired from a weapon.

View Folder On Github

Item Controller

This script handles what an item pickup does whenever a player collects it. Item pickups have a box collider set to IsTrigger and will get removed from the scene as soon as the player's collider overlaps with them.

When the item is collected it will change one of the player's stats depending on the value of the Stat Changed enumerator. The amount of change is then defined by the "Increased Amount" variable.

A sound effect is played when the item is collected and another one if the player is already at full capacity. A text message will also be displayed on the screen after pickup or "already full". If the "Collect at full Capacity" checkbox is turned on then you can collect the item anyway even if it won't increase the stat any further.

The particle index variable refers to which particle system on the PlayerController should player after the item is collected.

View Source File


Projectile Controller

This is what causes a projectile prefab to move after it has been instantiated. projectiles use a Rigidbody component to fly through the air and travel speed variable defines how fast it moved. The fire game object is defined at runtime after the player or an enemy fires the shot.

Whenever a projectile collides with something it is destroyed. But if it was fired by the player and hit an enemy or fired by the enemy and hits a player, the projectile then deals damage to the character's health.

View Source File


GUI Scripts

This folder contains all scripts that control the game's graphical user interface. This includes the main menu and the game options screen as well as the heads-up-display (HUD) that appears in front of the camera while playing the game.

View Folder On Github

GUI Controller

This script handles the changing of all information that appears on the player's Heads Up Display (HUD) as well as the showing and hiding of all other text and GUI elements when running the main game scene.

All of these objects are assigned to this script and there are variables for the time period that the wave start text and wave end text appears before turning off. There are also time frames for how long the item pickup and item run out message display.

During each frame update, the GUIController script will update the text on the HUD then update the health then update the jetpack fuel then update the armour. There are also a series of public functions which turn the pause and options menu on and off as well as show the wave start text, the wave completion text, the level complete panel and the game over panel.

When the health is 60% or more the bars inside the battery are green, if they are between 40% and 60% they are yellow and if they are less than 60% they are red. Armour and jetpack fuel both use bars that have their width scaled according to the current value and maximum value.

View Source File


Menu Controller

This is a script that is used in the Main Menu scene. It contains a series of public functions that are invoked whenever a button is clicked. Loading the Main scene opening the game options, going back to the main menu and quitting the game are all handled here.

View Source File


Game Options

This script handles the changing of all game options that can be changed while the program is running. This includes video settings such as switching from full screen to windows mode, the screen resolution, the graphics quality and whether vertical sync is on or not. It also includes volume controls for changing the master, music, sound effects and ambient volumes.

has all of the GUI elements assigned to it that appear on the game options screen which can be accessed from both the main menu and the pause menu.

Whenever one of the options is changed on the game options GUI a public function will be invoked on this script from the OnValueChanged event attached to that component. Whenever the save setting button is clicked the setting gets saved to the player's preferences. As soon as the scene loads again values get loaded from the player's preferences and get applied to the settings. Whenever the game options screen opens InitGUI() is invoked which updates the UI to match the save settings.

View Source File


Weapons & Enemy Waves Scriptable Objects

This folder contains scripts with classes that extend Unity's ScriptableObject class. This includes the different types of weapons that the player has as well as the configuration for the enemy waves that appear when playing the game.

View On Github

Projectile Weapon

This is a class that extends the Weapon scriptable object class. It stores values for the maximum ammo the gun can store, the ammo it starts with, the amount of damage each projectile does when it hit's an enemy and the time delay between each shot. The projectile prefab that gets spawned when firing is also defined here.

If "Rapid Fire" is turned on then the weapon will continue to fire when the fire button is held down otherwise it will only fire one shot per press.

View Source File


Laser Weapon

This is another class that extends the Weapon scriptable object class. It stores values for the maximum ammo the gun can store, the ammo it starts with, the amount of damage the laser does at each damage interval, the maximum range that the laser can fire and the delay between each damage interval. A material to use for the laser can also be assigned.

View Source File


Enemy Wave

This is where each of the enemy waves in the game is set up. Once an enemy wave object is created the number of enemies in a wave is then defined. Then for each element, the type of enemy to be spawned is selected, along with the spawn point number to spawn at the time period from the number of seconds from the beginning of the wave in which it should be spawned.

All of this information is then handled via the Game Manager on the Enemy Waves array.

View Source File


Leave a Reply