5

Introduction on Tower Defence Project:

This is my first university project within second year. It had to be a tower defence game that we wanted to create.
I chose to make a Online CO-OP Tower Defence game where you could help each other to defend against the military that is trying to attack you.
The theme is military I picked a simplistic style as that is what I was most tower defenses go and it looked good.

Latest video:


Multiplayer show off:




Things I learnt within this project:

Networking:

For networking I was using Mirror Networking which is an Networking API that goes over the top of Unity's UNet system to make it better/easier for developers to implement it.

This is the code for syncing an variable between all clients.
[SyncVar]
int test = 0;
This code allow you to fire a callback whenever this updates on *any* clients
[SyncVar=(hook = nameof(setTest))]
int test = 0;

void setTest(int oldTest, int newTest)
{
	// Do stuff here.
}
This made it very easy for me to be able to network variables however you have to make sure that you account for that value not being synced if you join the game after it is called.

Networking between Client -> Server needed a Command think of this like a message telling the server what to do. Some of these messages needed authority to work on certain objects so that people cannot control the wrong person
[Command]
public void CmdSetDoorState(DoorState newDoorState, NetworkConnectionToClient sender = null)
{
	if (sender.identity.GetComponent<Player>().hasDoorKey)
		doorState = newDoorState;
}

This code will allow only the owner of the door to open the door. Something like this has to have a specific hierarchy or a function called on it so that you can "give" it auth. Another way is to tell the command to ignore who the owner is.
[Command(ignoreAuthority = true)]

This required me to do the hierarchy like this so that everyone objects under the player gets the auth to control itself.


Other blogs about this project:

Object Pooling

The issues with using Object Pooling in a fully multiplayer game.

Read more