Defense Structures

👍

Reminder - Keep Daily Backups

When working on a strategy game with a kit as big as the Complete Kit - always keep a working daily backup! Save yourself the trouble of rolling-back changes and losing work.

What are defense structures?

The city building kit comes with defense building examples. These, like the Cannon, Archer Tower, and Catapult defend your village from attacking players and protect your base resources from being stolen.

748

Examples of the defense structures in the store.

Can I add my own defense structures?

Yes. In addition to customizing the existing 3 examples, we've added 3 placeholders for defense structures you can customize and setup on the bottom half of the menu.

748

Placeholders for new defense structures

You could add more defense structures extending this menu even further. For more details about the defense structure shop menu, please read the Shop Menu > Defense Weapons documentation page.

Where to find the defense store menu in Unity?

Open the game scene hierarchy and locate the ScrollView in Canvas > ModalsWindows > ShopWindows > ShopGroup > StorePanel > BottomGroup > Content > Content. Look at the below screenshot.

403

UIAnchor. Click to view larger.

Where are the prefabs?

All of the game object prefabs are set in the ScriptableObjects at the path Assets/CityBuildingKit/Resources/Assets/
For example take a look at the WeaponCategory.asset file below :

532

Inspector Defense prefabs. Click to view closer.

In addition, you will want to look at GameManager > Creators > WeaponCreator (prefabs are also there) This is discussed in the Buildings > Part 2: Customize Menus section of the documentation. Prefabs are used by the creator for construction and by SaveLoadBattle for loading during battle.

GameManager > Creators > WeaponCreator

In the GameManager > Creators > WeaponCreator we have the list of game object prefabs used by the creator in addition to certain build settings used by the Scripts/Creators/BaseCreator.cs script. Let's describe these below. But first, here's how to open the WeaponCreator Inspector.

276

Game scene Game Manager

370

Defense prefabs in the WeaponCreator Inspector

In the Inspector you'll notice a few arrays, here's a description of each one. It's the same for every type of game object you can build in the game except for the RemovableCreator (removables are not for players to construct but rather remove -- see Player Removable Objects doc for details)

Array TitleDescription
Structure PfThe structure prefab.
Grass Pf1x1 to 5x5 dark green grass collider grid prefabs list. These you don't have to edit, they are shared by all structures in the game based on their grid size.
Construction PfUnder construction prefab list. There's 3 different sizes of this for 1x1 to 3x3 grid size construction projects.
Grass TypesSize of this array matches the the Structure Pf array size. We tell which Grass Pf element to use with each structure. Depends on structure size whether they take 1x1 (Grass Pf element 0) like a wall segment or 3x3 (Grass Pf element 2) like the large gold vault. Tatami is the only object that takes grass collider 5x5 ( Grass Pf Element 4).
Construction TypesArray size matches Structure Pf. Like grass types, we match the construction prefab grid size too. Since there's 3 different sizes of construction prefabs, this ranges from values 0 to 2. By default, we set most structures to use the 3x size.
Pivot CorrectionsArray size matches Structure Pf. Odd size grid structures (1x1 and 3x3) need a pivot correction so they don't appear accidentally off-center, taking more grid space than necessary. Pivot Correction does this.
Is ArrayArray size matches Structure Pf. If set to 1, uses the row making field builder for construction instead of the usual construction. The only objects that use this are the wall segments that let you pick a start and end point and automatically constructs a repeating row of the structure between the points.
Is InstantArray size matches Structure Pf. For items you set TimeToBuild to 0 (instant build) in the SO, you also want to set the elements Is Instant boolean to 1 in this array. The only objects that use this in the demo are the wall segments.

Where are the defensive weapon scripts?

The scripts for defense weapons are all located in the Scripts/Weapons folder.

TurretControllerBase.cs

This script controls the rotation, target addition, and projectile creation among other elements of the defense structure.

// TurrentControllerBase.cs Excerpt Functions

	protected void LaunchProjectile()
	{
		((SoundFX)soundFx).CannonFire();

		GameObject projectile = Instantiate(projectilePf, projectileSpawnPointTr.position, projectileSpawnPointTr.rotation);
		_projectile = projectile.GetComponent<Projectile>();
		_projectile.DamagePoints = ((IDamagePoints) _structure).GetDamagePoints();
		_projectile.DamageType = ((WeaponCategory) _structure).GetDamageType();
	}
		
	public void AddTarget(GameObject target)
	{
		targetList.Add (target);
	}
	
	public void RemoveTarget(GameObject target)
	{
		targetList.Remove (target);
	}

PerimeterCollider.cs

This script determines the perimeter around the defense structure and whether units have entered the range of a defense structure. When they enter the collider range, OnTriggerEnter() runs which adds the game object to the list of targets the turret controller is attacking.

// Scripts/Weapons/PerimeterCollider.cs
//
// The following function triggers when a unit gameobject
// has come within range of the defense structure colliders

	void OnTriggerEnter(Collider collider)//OnCollisionEnter(Collision collision)
	{
		if (collider.gameObject.tag == "Unit") {	 
			collisionCounter++;						
			((TurretControllerBase)turretController).AddTarget(collider.gameObject);
			((TurretControllerBase)turretController).fire = true;
			inCollision = true;	
		}

	}

When the game object exits the collider range, OnTriggerExit() runs which removes the game object from the list of targets the turret controller is attacking.

// Scripts/Weapons/PerimeterCollider.cs
//
// This function triggers when the unit exits the weapon range
// it removes the target from the list of gameobjects the defense
// structure will attack
// 
	void OnTriggerExit(Collider collider)//OnCollisionEnter(Collision collision)
	{
		if (collider.gameObject.tag == "Unit") {	 
			collisionCounter--;	

			((TurretControllerBase)turretController).RemoveTarget(collider.gameObject);

			if(collisionCounter == 0)
			{		
				inCollision = false;

				if (transform.parent.GetComponent<StructureSelector> ().structureType == "Catapult") 
				{
					transform.parent.GetComponent<WeaponAnimationController> ().StopAnimations ();
				}
			}
		}
	}

Projectile.cs and Arrow.cs

The scripts for both the cannon projectiles and the archer tower arrows. These scripts instantiate the projectile collision and unit death.

// Scripts/Weapons/Projectile.cs Excerpt
//
// The following function triggered by PerimeterCollider.cs
// which determines when a unit enters the range of a weapon
// and has been hit by the projectile from the defense structure

    void OnTriggerEnter(Collider target)
    {
	    if (target.gameObject.CompareTag("Unit"))
	    {
		    positionForInstantiation = target.transform.position;
		    _fighterController = target.GetComponent<FighterController>();
		    _index = target.GetComponent<Selector>().index;
		    _fighterController.Hit(DamagePoints, Instantiate);
	    }
    }

Defense Structure Example (Cannon)

Like many free-to-play strategy games, there are defense weapons like the below example included in the new City Building Kit version.

636

Defense weapon cannon example. Click to view larger.

WeaponCategory.asset Example for a defense structure

Here's an example of the simple cannon weapon in the ScriptableObject. The Scripts/Creators/BaseCreator.cs will use these settings.

532

If you add new weapon units or subtract some via the WeaponCategory.file , please don't forget to update GameUnitsSettings.asset file. Read more info at Documentation > XML files VS ScriptableObjects

Where can I edit the fire rate for defense weapons?

You'll need to edit the Fire Rate item value at the ScriptableObject for Weapons ( WeaponsCategory.asset )

For more details about the SO, please see the Shop Menu > Defense Weapons documentation.