Show Sidebar

Bolt Tutorial - Chapter 5

<< Prev Chapter

Authoritative Lag Compensated Shooting

The title is quite a mouthful, but so is what we are going to implement. The following chapters 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 a new C# script called TutorialWeapon.cs.

Inside this 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 do is to create one more script, right next to our TutorialWeapon, and call it TutorialWeaponRifle.

For now this is going to be mostly empty and simply inherit from our TutorialWeapon class.

Go into the bolt_tutorial/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.

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

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.

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

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.

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. First we need to add a pitch property to our TutorialPlayerState asset.

Name the property "pitch", make sure the type is Float. Configure it accordingly.

  1. Replicate When set to Value Changed
  2. Replicate To set to Everyone Except Controller
  3. Interpolate enabled

Time to compile Bolt, click to Assets/Compile Bolt Assets. 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 updated 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.

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 TutorialPlayerCommand and add a aiming and fire properties to the Input part, they should both be bools.

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

Go to the ExecuteCommand function inside TutorialPlayerController. In ExecuteCommand right after 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 (fireIntervall is calculated from our RPM setting on the weapon), if enough frames have passed we set the .fireFrame property again, and call into the mecanim 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, override the Bolt provided Attached method.

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 TutorialRifle 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