Chapter 5

<< Prev Chapter

Authoritative Lag Compensated Shooting

The title is quite a mouthful, but so is what we are going to implement. On this chapter we will handle one of those "holy grail" style things of network programming. We are going to demonstrate several key things.

  1. Authoritative weapon switching
  2. Authoritative shooting
  3. Lag compensation on clients

Guns, Guns And More Guns!

The first thing we are going to do is to set up a little bit of a generic Weapon component, create a new folder called Weapons in tutorial/Scripts and inside that create two new C# script called TutorialWeapon.cs and TutorialWeaponRifle.cs.

*TutorialWeapon* and *TutorialWeaponRifle* scripts
TutorialWeapon and TutorialWeaponRifle scripts.

Inside the TutorialWeapon script we are going to put a pretty standard unity mono behaviour, which is just going to have some variables for configuring our weapon, as this is pretty much standard Unity stuff we will not go into more details on it here. We will end up using all of these variables during the tutorial, so they will all be explained individually.

The next thing we are going to edit the TutorialWeaponRifle script. For now this is going to be mostly empty and simply inherit from our TutorialWeapon class.

Go into the bolt_samples/AdvancedTutorial/prefabs/weapons folder and find the Rifle prefab, create a duplicate of it (CTRL+D on Windows, CMD+D on OS X). The duplicate will be called "Rifle 1", drag it into our tutorial/Prefabs folder and rename it to TutorialRifle.

*TutorialRifle* prefab preparation
TutorialRifle prefab preparation.

Select our new TutorialRifle prefab and add our TutorialWeaponRifle script to it.

*TutorialRifle* prefab with our *TutorialWeaponRifle* script
TutorialRifle prefab with our TutorialWeaponRifle script.

Lets hook up all of the public variables on the TutorialWeaponRifle through the inspector, instead of trying to explain exactly what goes where, here's a picture on how to hook everything up properly.

*TutorialRifle* prefab setup.
TutorialRifle prefab setup.

Time to get our little soldier to hold his weapon. Select our TutorialPlayer prefab (1). The rifle should be rotated properly and you should just have to drop your TutorialRifle prefab it under his right hand like below (2). Don't forget to save the TutorialPlayer prefab by either clicking Apply (3) or dragging it back on-top itself in the Project window.

*TutorialPlayer* prefab setup with the *TutorialRifle* object
TutorialPlayer prefab setup with the TutorialRifle object.

If you play the game you should see the rifle in your characters hands. If it is rotated incorrectly go back to the TutorialPlayer prefab and re-adjust it.

Game running. *TutorialPlayer* with the *TutorialRifle*
Game running. TutorialPlayer with the TutorialRifle.

As you might have noticed it is not possible to pitch the camera, this is simply because we have made the PlayerCamera class we are working generic enough to work with the tutorial code in it's non-finished state. Open the Window/Bolt/Assets window and select the TutorialPlayerState state, we need to add a pitch property to it.

New *pitch* property on *TutorialPlayerState*
New pitch property on TutorialPlayerState.

Follow the instructions:

  1. Create a new property on the TutorialPlayerState asset;
  2. Rename the property to pitch;
  3. Change the type to Float;
  4. Set Replication property to Everyone Except Controller;
  5. Set Mecanim to Disable;
  6. Set Smoothing Algorithm to Interpolation;
  7. Set Interpolation Mode to As Float.

Taking advantage that we are already editing the state the player, we will add one more property, named Fire. This property will signal when our player is firing his weapon.

New *Fire* property on the *TutorialPlayerState*
New Fire property on the TutorialPlayerState.

Follow the instructions:

  1. Create a new property on the TutorialPlayerState asset;
  2. Rename the property to Fire;
  3. Change the type to Trigger;
  4. Set Replication property to Everyone Except Controller;
  5. Set Mecanim to Parameter - Using Bolt Properties.

Time to compile Bolt, click to Assets/Bolt/Compile Assembly. Let Bolt do it's magic and then open up the TutorialPlayerController script. We are going to update the ExecuteCommand method, inside the if-block protected by the cmd.isFirstExecution check. We're going to add a line which copies the pitch from our command into our state.

We can then go to our TutorialPlayerCallbacks script and update the ControlOfEntityGained method so it looks like this.

The reason for this little roundabout way of getting the pitch into the camera is so that the camera can work without having all of the states compiled from the beginning, it allows us to progress in the tutorial in a more natural way.

We also add an AudioListener component to our entity game object so that we hear from the characters perspective.

It's time to hook up our weapons and get to some shooting. First we need a way to find the weapons in ExecuteCommand, on the TutorialPlayerController script add a new inspector variable called weapons and have it be an array of TutorialWeapon objects, like this:

In the Unity inspector drag the TutorialRifle object attached to the right hand of our character to the new weapons field on the TutorialPlayerController. Don't forget to apply your changes.

Setup weapon reference on *TutorialPlayer* prefab
Setup weapon reference on TutorialPlayer prefab.

We need a couple of more pieces of input on our command so that we can properly communicate that we are firing our weapon. Open up the Window/Bolt/Assets window and click on theTutorialPlayerCommand. Add a aiming and fire properties to the Input part, both should be booleans.

New *weapon* properties on the *TutorialPlayerCommand* asset
New weapon properties on the TutorialPlayerCommand asset.

Compile Bolt again (Assets/Bolt/Compile Assembly) and open up our TutorialPlayerController script again, inside PollKeys we are going to query the state of our left and right mouse buttons.

Inside the SimulateController function, we also need to put these values into the command that will be queued to the system, for later use. Update your script and add the two following lines:

Now go to the ExecuteCommand function and right next where we copy our pitch from the command input to the state property, add a check to see if both aiming and fire are pressed down and if they are call the FireWeapon function (which we are going to create).

Create a function called FireWeapon which takes a TutorialPlayerCommand class as its only argument.

Since we only have one weapon currently we just index straight into the weapons array, we check when we last fired plus how many frames that have to pass between each shot (FireInterval is calculated from our RPM setting on the weapon), if enough frames have passed we set the FireFrame property again and call into the state.Fire() trigger.

The reason we are using mecanim triggers for communicating the fact that we have fired our weapon is that they are incredibly light weight, it will in fact only use two bits and since we are firing quite often sending a comparatively large event is not worth it.

So, we need a way to hook into this mecanim trigger, fortunately Bolt lets you hook into mecanim and get a callback whenever a trigger is raised. Still inside of the TutorialPlayerController script, append to your Attached method the lines:

We simply attach a C# lambda method to the OnFire callback on the mecanim state and in that we call into the DisplayEffects method of our weapon. We are not quite done yet, but here is the completed TutorialPlayerController script for reference.

The only thing left now is to implement the DisplayEffects function on our TutorialWeaponRifle script, this function is quite large, but there is nothing Bolt specific going on inside of it, as its just using plain Unity methods to show some fancy effects, etc.

You should now be able to play as server and any number of clients and all firing and effects will replicate properly.

 To Document Top