Mittwoch, 9. Mai 2012

Unity3D: Half-Authoritative Network Shot

I wanted to create shots on the network. First thing I thought at, was to use a shot prefab with a NetworkView attached to it and to instantiate every shot on the network with Network.Instantiate().

But that does much overhead if you use a machine gun for example, wich fires more than 10 shots a second. So I decided to remove the NetworkView and RPC's and to just create the shots locally depending on an isFiring-flag.

The next problem I ran into was health draw on hit players. Imagine there is lag and the player on the server already moved further away than the (hit) player on the client. The client then draws health but not the server.

So I  updated the shots collision code. It applies damage only on the servers player object, wich then sends an RPC to every player-on-a-client instance to synchronize the damage.

Now, this is how a shot will be created and passed to the network:

1. If the player hits the fire button, an RPC will be sent to the network setting the isFiring-flag to true.
2. Every client AND the server create the shots locally when the isFiring-flag is true.
3. If a shot on a client hits something, it just disappears.
4. If a shot on the server hits something, it calls the ApplyDamage-function on the hit player. (If it is a player ;) )
5. The ApplyDamage function itself just calls an RPC called doDamage.
6. The doDamage (-RPC-) function draws now the amount of health we are demanding. This happens similarily on the server and on all clients.
(7.) If the player releases the fire button, an RPC will be sent to the network setting the isFiring-flag to false.

I will post the code later on. But I hope you got it anyway. ;)

Please comment if you have another/better solution for network shooting code.

Keine Kommentare:

Kommentar veröffentlichen