Repository navigation
Technical Design
#Technical designs See Byron's Technical designs
De animaties zijn geïntegreerd via scripts in de player movement script en de player attack script. Als de speler beweegt, dan krijg je een animatie dat de speler loopt, als de speler via de muis aanvalt, doet de speler een aanval animatie. De animaties zijn anders voor andere classes, zoals voor de archer en de knight, zijn de animaties anders, want de knight hoeft alleen te zwaaien met zijn zwaard en voor de archer heb je dat de speler eerst een arrow pakt en dan gaat aimen voordat het aanvalt.

De bovenste script is het script voor de movement, als de speler beweegt, gaat de speler ook bewegen met de animatie waarvan het ook nodig is. De script van beneden is hoe de speler de animatie gebruikt voor bijvoorbeeld de melee attack, of de archer attack.
On wave 1 each zone will have the exact same amount of zombies that it will spawn. It'll use a for loop so it iterates through the total spawn amount. It uses a random number generator with its ranges being the the minimum and maximum their zone then it will spawn until it has spawned the amount of enemies its required to.
graph TD;
Start((Start))--> Wave-starts
Wave-starts--> Get-current-wave;
Get-current-wave--> Calculate-enemies-Count
Calculate-enemies-Count-->Spawn-enemies
Add-Health-Correlating-To-Wave-->Spawn-enemies
Spawn-enemies-->Killed-all-enemies?
Killed-all-enemies?--> |Safe period| Wave-starts;
The enemy then gets an script for targeting, movement and attacking, within the targeting script, the script will target the nearest structure it finds, if the player comes near the enemy, it will target the player.
For the Enemy movement script, it will use the navmesh to get to the nearest structure for the enemy to attack, if the player gets too close to the enemy, the enemy will follow the player so it can attack the player.
When the Enemy uses the Attacking script, it will give the player or the structure it hits damage. If the player or structure has a health value of 0 or below, the player dies or the structure collapses.
graph TD;
Start((Start)) --> Enemy_Spawns;
Enemy_Spawns --> Is_Player_InRange;
Is_Player_InRange --> |Yes| Player_Target;
Is_Player_InRange --> |No| Get_Nearest_Structure;
Player_Target --> Move_To_Player;
Get_Nearest_Structure --> Find_Nearest_Structure;
Move_To_Player & Find_Nearest_Structure --> Is_Enemy_At_Target?;
Is_Enemy_At_Target? --> |Yes| Attack_Target;
Is_Enemy_At_Target? --> |No| Move_Towards_Target;
Move_Towards_Target --> Is_Player_InRange;
Attack_Target --> Target_Dead?;
Target_Dead? --> |Yes| Is_Player_InRange;
Target_Dead? --> |No| Continue_Attacking;
This script is used by every single object that needs health, like the player, enemy, structures or the main base.
This script gets used to damage to damage each object that are bound to categories, the Enemy, the Player, the Structure and the main base. the Enemy deals damage to the base, structures or player, the Player deals damage to enemies, and some structures can deal damage to the enemy.
graph TD;
A[Player] -->|DealDamage| B[Enemy]
C[Structure] -->|DealDamage| D[Enemy]
E[Enemy] -->|DealDamage| F[Player]
E -->|DealDamage| G[Structure]
E -->|DealDamage| H[Base]
De Healthbar wordt gebruikt samen met de universal health, wat de speler, main gebouw, andere gebouwen of enemies kunnen gebruiken, zodat ze niet bijvoorbeeld na een hit dood gaan. Als het HP bij de healthbar op 0 is, gaat degene die er aan is geassigned met deze healthbar dood gaat. Verder is het ook mogelijk om een maximale HP threshold te geven aan degene die de healthbar krijgt. Verder als degene die de healthbar heeft damage krijgt, dan veranderd de healthbar ook met de hoeveelheid health het nog heeft.

This script gets used when the player is on an placeholder place, where they are able to buy structures from the shop, if the player already has placed an structure on there, they can't create another structure on the same place.
This script gets used when the enemy is close in range of the structure, the structure will then shoot at the enemy that would deplete the health. It will only target the first enemy target, and not the 2nd one that is in range for now.
A script that handles the ping system: pings are GameObject prefabs that will be instantiated when you call a function
Made by: Daan

The first iteration of the enemy system was functional, however it was buggy and didn’t allow for any enemy variation. Therefore we decided to make a 2nd iteration. This system had its movement, targeting and attacking divided into 3 separate scripts, and was altered to use the universal weapon system. This new enemy system allowed for ranged and melee enemies to be made using the same scripts, and also added the ability to select what objects the enemy should target (In case we wanted an enemy that only focused on the base instead of the player, or vice versa). The enemy still used Unity's navmesh system for their pathfinding as this was the quickest and easiest way to do it.
This flowchart describes the enemy's intended behaviour

Made by Byron
We want the player do have different type of playstyles. Classes will have different weapons depending on their type, ranged or close range for example. Classes have a progression aspect to them each level will give their stats a little boost aswell as unlock new weapons. We believed it would be fun to have different ways of defending the base.
The EnemyMovement script is where the scripts communicate with the NavMeshAgent component to tell the enemy where and when to move. The SetTarget function is used to communicate the current target to the NavMeshAgent. In the Update function the script checks wether or not it has reached its set stopDistance from the target. When it has the CheckLineOfSight function will start to be called, this checks if there are any obstacles between the enemy and the player, if there are the enemy will continue to move towards the player. If the enemy is within its stopping distance and has line of sight to the player the StopMove coroutine is called. This coroutine stops the player after a delay, otherwise ranged enemies would often stop halfway behind a wall and shoot it instead. When the enemy is stopped the FaceTarget will quickly lerp the enemy rotation towards the target, and keep doing so untill the target leaves its stopping distance/line of sight.
The EnemyTargeting is where the target for the enemy is determined. On start the ResetTargetToMainBase is called so that the enemy will always spawn with a target. The DetectPlayers function uses an OverlapSphere to check if any players are close, and there are, will call the SetNearestPlayerAsTarget function with the player as a parameter. If the function stops detecting players ResetTargetToMainBase is also called. SetNearestPlayerAsTarget First checks to see if there are multiple players nearby. If there is only a single player then that player will be set as the _enemyTarget. If there are multiple players in range, the function will instead check which is the closest and set that as the target. The UpdateTarget function is required for the navemesh agent to follow its target if it moves as they do not do so by default. UpdateTarget simply communicates the current target's position to the navemesh agent every few frames, and also calls the GetTargetEdge function beforehand. GetTargetEdge will get the closest edge of the target's hitbox instead of its center so that the enemy doesn't end up walking against the target and doing nothing. GetTargetEdge then sets the enemy's target to the calculated position.
The EnemyAttack script does not do a lot by itself, but is made to work with the universal weapon system. The Start function calls the SpawnWeapon function on the weapon ScriptableObject thats given to the script to initialize the weapon. The Attack function calls the attack function on the scriptable object aswell.

The EnemyReferences script simply holds a few references that are required in more than one script to avoid having to do this for each script individually.

In this game we wanted to handle the attacking using weapons. This was so that the same system could be used for all things that needed to attack, and so that all those things needed a single attack script. We wanted these weapons to be easily adjustable and make it easy to add more of them. For this reason I decided to use Unity's ScriptableObjects. This way we could make a weapon in a few clicks and easily drag and drop them onto whatever we wanted. The Weapon parent class has all the necessary functions and variables that the scripts using the weapon need to interact with. Then I made the RangedWeapon and MeleeWeapon classes that inherit from Weapon.

This is the parent ScriptableObject class from which all weapon classes inherit.
The SpawnWeapon function instantiates the model and sets its position to the parent given to the script. DespawnWeapon destroys the model. Attack is to be defined in the classes that inherit from this class, it is defined here so that an Attack script can call the Attack function on every kind of weapon without having to change the script. SetAttackDirection sets the position where the weapon attack should be done and also sets its direction towards the mouse position.
public abstract class Weapon : ScriptableObject
{
[Header("Weapon stats")]
[SerializeField] protected int damage;
[SerializeField] public float attackDelay;
[Header("Visuals")]
[SerializeField] protected GameObject weaponModel;
public GameObject hitboxHolder;
protected Vector3 p_attackSpawnPosition;
protected Quaternion p_attackDirection;
protected GameObject p_model;
public virtual void SpawnWeapon(Transform parent)
{
p_model = Instantiate(weaponModel);
p_model.gameObject.transform.SetParent(parent);
p_model.transform.position = parent.transform.position;
p_model.transform.rotation = parent.transform.rotation;
}
public virtual void DespawnWeapon()
{
Destroy(p_model);
}
public abstract void Attack();
public void setAttackDirection(Quaternion dir, Vector3 pos)
{
p_attackDirection = dir;
p_attackSpawnPosition = pos;
}
}The RangedWeapon class inherits from the Weapon class. The only thing this class changes is that it adds a projectile variable and that it overrides the Attack function to instantiate the projectile in the attack direction.
public class RangedWeapon : Weapon
{
[Header("Projectile settings")]
[SerializeField] private GameObject projectile;
public override void Attack()
{
Instantiate(projectile, p_attackSpawnPosition , p_attackDirection);
}
}The MeleeWeapon class is a little more complicated. It overrides the SpawnWeapon function to additionally spawn a Collider for the melee attack and also to apply a script to that collider that handles dealing damage. DespawnWeapon also additionally destroys those two components. In the Attack function I wanted to use an Coroutine to toggle the collider, however I quickly realised that this is impossible in this script itself as that requires Monobehaviour. To get around this is I made a simple script called WeaponMonoInstance that I would use to handle this coroutine instead. The ToggleHitbox function simply toggles the hitbox on and off.
public class MeleeWeapon : Weapon
{
[Header("Hitbox Settings")]
[Tooltip("The amount of time the hitbox should be active")]
[SerializeField] private float hitBoxDuration = 0.1f;
[Tooltip("How far the hitbox should be placed in front of the player")]
[SerializeField] private float hitboxOffset = 1;
[SerializeField] private Vector3 hitboxScale = new Vector3(1, 1, 1);
private BoxCollider _hitbox;
private WeaponMonoInstance _monoInstance;
private DealDamageOnCollision _damageScript;
public override void Attack()
{
if (WeaponMonoInstance.instance != null)
WeaponMonoInstance.instance.StartCoroutine(ToggleHitbox());
}
private IEnumerator ToggleHitbox()
{
if(_hitbox != null)
_hitbox.enabled = true;
yield return new WaitForSeconds(hitBoxDuration);
if(_hitbox != null)
_hitbox.enabled = false;
}
public override void SpawnWeapon(Transform parent)
{
base.SpawnWeapon(parent);
_damageScript = hitboxHolder.AddComponent<DealDamageOnCollision>();
_hitbox = hitboxHolder.AddComponent<BoxCollider>();
_damageScript.SetDamage(damage);
_hitbox.size = hitboxScale;
_hitbox.center = new Vector3( 0, 0, hitboxOffset);
_hitbox.isTrigger = true;
_hitbox.enabled = false;
}
public override void DespawnWeapon()
{
base.DespawnWeapon();
Destroy(_hitbox);
Destroy(_damageScript);
}
}The attack scripts are all very similar. In the Start function they call the SpawnWeapon function on from the weapon given to the script and give it a spawnPoint. The Update function is where the attack function is called depending on either input for the player or a condition for enemies. There is also a timer that ticks down in Update. the Attack function in all of these scripts first checks to see if the _attackTimer is zero. If it is zero then it will first call the weapon's SetAttackDireciton function, and after it will call the weapon's Attack function. It will then set the _attackTimer equal to the weapon's attackDelay value.
The turning of the player will use an raycast. The cursor will aim at a point in the scene it'll return a vector 3 which I can use to subtract from the current position so I know what the direction/offset is of the way I want to aim.