Who shot that? Networking Projectiles.

Who gets the points?

In my previous article I talked about how you can achieve better performance by creating projectiles locally when compared with creating a server instance and keeping them synchronised with the clients. But what happens when the projectile actually hits something, who gets the points associated with killing the enemy?

Projectile Ownership

Whilst there are some subtleties in scoring (e.g. if player 2 hits the big baddy 99/100 times but player 1 has the last shot, how do you score that?) the basic problem is knowing who fired the shot that hit the enemy. In Xevious my projectiles have an IProjectile interface/contract. Therefore I added an owner network Id.

When the ship fires a bullet (a projectile) it previously called the Launch method of the bullet. Now it has changed to also pass in the Network Id of the ship that fired it. The bullet has an owner that we can trace back to ship and therefore the player.

Player Score UI

Whilst we know the owner of the bullet, the UI needs to be able to use this information.

When the ship is created AND we are the Client then we tell that client’s UI Manager the network Id of the ship.

When the UI Manager is asked to increase the score it compares the owner of the projectile and only increases the score if there is a match.

Therefore each player can be attributed the score value of the enemy they hit.

What next?

I need to add the same functionality to bomb projectile, currently it is not working as it always sends a 0 owner id. That should be a trivial change. However, I also need to network the bombs, I suspect that because you are only allowed to fire one bomb at a time that this might be better to implement as a server instanced and synced object. Decisions, decisions…

Unity, VR, Enterprise and .Net Developer